Skip to content

Commit 951ccc1

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

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

140-word-break-ii.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@
55
*/
66
const wordBreak = function(s, wordDict) {
77
const set = new Set(wordDict)
8-
return helper(s, 0, set)
8+
const map = new Map()
9+
return helper(s, 0, set, map)
910
};
1011

11-
function helper(str, idx, set) {
12+
function helper(str, idx, set, map) {
1213
if(idx === str.length) return []
14+
if(map.has(idx)) return map.get(idx)
1315
const res = []
1416
for(let i = idx; i < str.length; i++) {
1517
const tmp = str.slice(idx, i + 1)
1618
if(set.has(tmp)) {
17-
const arr = helper(str, i + 1, set)
19+
const arr = helper(str, i + 1, set, map)
1820
if(i === str.length - 1) res.push(tmp)
1921
for(let item of arr) {
2022
res.push(`${tmp} ${item}`)
2123
}
2224
}
2325
}
26+
map.set(idx, res)
2427
return res
2528
}
26-
2729
// another
2830

2931

0 commit comments

Comments
 (0)