-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path140-word-break-ii.js
122 lines (112 loc) · 2.58 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @param {string} s
* @param {string[]} wordDict
* @return {string[]}
*/
const wordBreak = function(s, wordDict) {
const set = new Set(wordDict)
const map = new Map()
return helper(s, 0, set, map)
};
function helper(str, idx, set, map) {
if(idx === str.length) return []
if(map.has(idx)) return map.get(idx)
const res = []
for(let i = idx; i < str.length; i++) {
const tmp = str.slice(idx, i + 1)
if(set.has(tmp)) {
const arr = helper(str, i + 1, set, map)
if(i === str.length - 1) res.push(tmp)
for(let item of arr) {
res.push(`${tmp} ${item}`)
}
}
}
map.set(idx, res)
return res
}
// another
/**
* @param {string} s
* @param {string[]} wordDict
* @return {string[]}
*/
const wordBreak = function(s, wordDict) {
return backTrack(s, wordDict, {})
};
function backTrack(s, wordDict, mem) {
if(mem.hasOwnProperty(s)) return mem[s]
const result = []
for(let word of wordDict) {
if(s.startsWith(word)) {
let next = s.slice(word.length)
if(next.length === 0) result.push(word)
else {
for(let sub of backTrack(next, wordDict, mem)) {
result.push(word+ ' '+sub)
}
}
}
}
mem[s] = result
return result
}
// another
/**
* @param {string} s
* @param {string[]} wordDict
* @return {string[]}
*/
const wordBreak = function (s, wordDict) {
const dictSet = new Set(wordDict)
const memo = {}
function dfs(start) {
if (start > s.length - 1) {
return [[]]
}
if (memo[start] !== undefined) {
return memo[start]
}
const out = []
for (let i = start; i < s.length; i++) {
const substr = s.substring(start, i + 1)
if (dictSet.has(substr)) {
let next = dfs(i + 1)
for (let n of next) {
out.push([substr, ...n])
}
}
}
return (memo[start] = out)
}
const res = dfs(0)
return res.filter((a) => a.join('') === s).map((a) => a.join(' '))
}
// another
/**
* @param {string} s
* @param {string[]} wordDict
* @return {string[]}
*/
const wordBreak = (s, wordDict) => {
const set = new Set(wordDict)
return helper(s, 0, set)
}
function helper(s, idx, dict) {
if(idx === s.length) return []
const res = []
for(let i = idx; i < s.length; i++) {
const tmp = s.slice(idx, i + 1)
if(dict.has(tmp)) {
const arr = helper(s, i + 1, dict)
if(i + 1 >= s.length) {
res.push(tmp)
} else if(arr.length) {
for(let e of arr) {
res.push(tmp + ' ' + e)
}
}
}
}
return res
}