We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5ea42e3 commit 70d6f2dCopy full SHA for 70d6f2d
140-word-break-ii/140. Word Break II.py
@@ -0,0 +1,25 @@
1
+class Solution:
2
+ def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
3
+ # quick check on the characters,
4
+ # otherwise it would exceed the time limit for certain test cases.
5
+ if set(Counter(s).keys()) > set(Counter("".join(wordDict)).keys()):
6
+ return []
7
+
8
+ wordSet = set(wordDict)
9
10
+ dp = [[]] * (len(s)+1)
11
+ dp[0] = [""]
12
13
+ for endIndex in range(1, len(s)+1):
14
+ sublist = []
15
+ # fill up the values in the dp array.
16
+ for startIndex in range(0, endIndex):
17
+ word = s[startIndex:endIndex]
18
+ if word in wordSet:
19
+ for subsentence in dp[startIndex]:
20
+ sublist.append((subsentence + ' ' + word).strip())
21
+ print(sublist)
22
23
+ dp[endIndex] = sublist
24
25
+ return dp[len(s)]
0 commit comments