Skip to content

Commit f1d5e7d

Browse files
authoredApr 26, 2021
Update 139-word-break.js
1 parent 951ccc1 commit f1d5e7d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 

‎139-word-break.js

+31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
const wordBreak = function(s, wordDict) {
7+
const map = new Map()
8+
return helper(s, 0, new Set(wordDict), map)
9+
};
10+
11+
function helper(str, idx, set, map) {
12+
if(idx === str.length) return true
13+
if(map.has(idx)) return map.get(idx)
14+
let res = false
15+
for(let i = idx; i < str.length; i++) {
16+
const tmp = str.slice(idx, i + 1)
17+
if(set.has(tmp)) {
18+
const bool = helper(str, i + 1, set, map)
19+
if(bool) {
20+
res = true
21+
break
22+
}
23+
}
24+
}
25+
map.set(idx, res)
26+
return res
27+
}
28+
29+
// another
30+
31+
132
/**
233
* @param {string} s
334
* @param {string[]} wordDict

0 commit comments

Comments
 (0)
Please sign in to comment.