Skip to content

Commit c4af0e7

Browse files
committed
FEAT : solved word-break
1 parent 93bad17 commit c4af0e7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

word-break/aa601.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#시간복잡도 O(N * M)
2+
#공간복잡도 O(N)
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
5+
# 문자열 s의 0번째부터 i번째까지의 문자열이 wordDict의 단어로 분할될 수 있으면 True
6+
# 빈 문자열 s[0:0]은 항상 분할이 가능하므로 dp[0] = True
7+
dp = [False] * (len(s) + 1)
8+
dp[0] = True
9+
for i in range(1, len(s) + 1):
10+
for word in wordDict:
11+
# 현재 word가 i - len(word)부터 i번째까지의 s의 부분문자열과 일치한다면 True
12+
# 바로 이전의 지점이 True이고 word만큼의 s 부분문자열이 wordDict 내에 존재한다면 dp[i] = True
13+
if i >= len(word) and dp[i - len(word)] and s[i - len(word):i] == word:
14+
dp[i] = True
15+
break
16+
return dp[-1]
17+

0 commit comments

Comments
 (0)