Skip to content

Commit a952bec

Browse files
committed
adding dfs
1 parent 8bb6bc0 commit a952bec

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union
2+
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
5+
res = []
6+
wordSet = set(wordDict)
7+
8+
def helper(i, cur, path):
9+
if i == len(s):
10+
if not cur:
11+
res.append(' '.join(path))
12+
return
13+
14+
cur += s[i] # include current character
15+
16+
if cur in wordSet:
17+
helper(i + 1, '', path + [cur]) # cut here
18+
19+
helper(i + 1, cur, path) # continue building cur
20+
21+
helper(0, '', [])
22+
return res

0 commit comments

Comments
 (0)