-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy path140 Word Break II.js
53 lines (45 loc) · 1.5 KB
/
140 Word Break II.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// http://fisherlei.blogspot.com/2013/11/leetcode-wordbreak-ii-solution.html
/**
* @param {string} s
* @param {set<string>} wordDict
* Note: wordDict is a Set object, see:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
* @return {string[]}
*/
var wordBreak = function(s, wordDict) {
var result = [];
var solutions = [];
var len = s.length;
var possible = [];
for(var i = 0; i <= s.length; i++) {
possible.push(true);
}
getAllSolutions(0, s, wordDict, result, solutions, possible);
return solutions;
};
function getAllSolutions(start, s, wordDict, result, solutions, possible) {
if(start === s.length) {
solutions.push(result.join(' ')) // remove the last space
return;
}
// loop through string from i to s.length
for(var i = start; i < s.length; i++) {
var piece = s.substring(start, i+1);
// possible is to mark step whether i+1 to s.length have any possible words
if(wordDict.has(piece) && possible[i+1]) {// eliminate unnecessary search
result.push(piece);
var beforeChange = solutions.length;
getAllSolutions(i + 1, s, wordDict, result, solutions, possible);
if(solutions.length === beforeChange) {
possible[i+1] = false;
}
result.pop();
}
}
}
var dict = new Set();
dict.add('leet');
dict.add('code');
dict.add('cod');
dict.add('de');
wordBreak('leetcode', dict)