Skip to content

Commit 70d6f2d

Browse files
committed
Runtime: 39 ms (25.55%), Memory: 16.6 MB (10.04%) - Gitcode
1 parent 5ea42e3 commit 70d6f2d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)