-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path09_word_break.py
More file actions
31 lines (22 loc) · 815 Bytes
/
09_word_break.py
File metadata and controls
31 lines (22 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
dp = [False] * (len(s) + 1)
dp[len(s)] = True
for i in range(len(s) - 1, -1, -1):
for w in wordDict:
if (i + len(w)) <= len(s) and s[i : i + len(w)] == w:
dp[i] = dp[i + len(w)]
if dp[i]:
break
return dp[0]
if __name__ == "__main__":
obj = Solution()
s1 = "leetcode"
wordDict1 = ["leet","code"]
print(obj.wordBreak(s = s1, wordDict = wordDict1))
s2 = "applepenapple"
wordDict2 = ["apple","pen"]
print(obj.wordBreak(s = s2, wordDict = wordDict2))
s3 = "catsandog"
wordDict3 = ["cats","dog","sand","and","cat"]
print(obj.wordBreak(s = s3, wordDict = wordDict3))