Skip to content

Commit 95965ef

Browse files
authored
Update 140-word-break-ii.js
1 parent 6fd5ea7 commit 95965ef

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

140-word-break-ii.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {string[]}
5+
*/
6+
const wordBreak = function(s, wordDict) {
7+
const set = new Set(wordDict)
8+
return helper(s, 0, set)
9+
};
10+
11+
function helper(str, idx, set) {
12+
if(idx === str.length) return []
13+
const res = []
14+
for(let i = idx; i < str.length; i++) {
15+
const tmp = str.slice(idx, i + 1)
16+
if(set.has(tmp)) {
17+
const arr = helper(str, i + 1, set)
18+
if(i === str.length - 1) res.push(tmp)
19+
for(let item of arr) {
20+
res.push(`${tmp} ${item}`)
21+
}
22+
}
23+
}
24+
return res
25+
}
26+
27+
// another
28+
29+
130
/**
231
* @param {string} s
332
* @param {string[]} wordDict

0 commit comments

Comments
 (0)