Skip to content

Commit e23a34e

Browse files
author
jinbeom
committed
Word Break Solution
1 parent f415907 commit e23a34e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

word-break/kayden.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 시간복잡도: ?
2+
# 공간복잡도: O(S)
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
5+
memo = {}
6+
wordSet = set(wordDict)
7+
8+
def dfs(idx):
9+
if idx in memo:
10+
return memo[idx]
11+
12+
if idx == len(s):
13+
return True
14+
15+
for word in wordSet:
16+
l = len(word)
17+
if s[idx:idx + l] == word:
18+
if dfs(idx + l):
19+
memo[idx] = True
20+
return True
21+
22+
memo[idx] = False
23+
return False
24+
25+
return dfs(0)

0 commit comments

Comments
 (0)