-
Notifications
You must be signed in to change notification settings - Fork 1
/
substring-with-concatenation-of-all-words.ts
242 lines (198 loc) · 6.15 KB
/
substring-with-concatenation-of-all-words.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// LeetCode 30. 串联所有单词的子串 https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
// LintCode 1362. 包含所有单词连接的子串 https://www.lintcode.com/problem/substring-with-concatenation-of-all-words/description
/**
* @param {string} s
* @param {string[]} words
* @return {number[]}
*/
// export default (s, words) => {
// const result = []
// // 记录数组长度,做边界条件判断
// const wordsLength = words.length
// const range = (r, _arr) => {
// if (r.length === wordsLength) {
// result.push(r)
// } else {
// _arr.forEach((item, index) => {
// const tmp = [].concat(_arr)
// tmp.splice(index, 1)
// range(r.concat(item), tmp)
// })
// }
// }
// range([], words)
// const indexs = []
// result.forEach(item => {
// let tmp = null
// do {
// tmp = s.indexOf(item.join(''), tmp === null ? 0 : tmp + 1)
// indexs.push(tmp)
// } while (tmp !== -1)
// })
// return [...new Set(indexs.filter(item => item !== -1))].sort()
// }
// // 效率较高但仍跑不了很长的测试用例
// export default (s, words) => {
// if (s === '' || words.length === 0) return []
// const codeSum = (str) => {
// let sum = 0
// for (let n = 0; n < str.length; n++) {
// sum += str[n].charCodeAt()
// }
// return sum
// }
// const wordsStr = words.join('')
// const wordLength = wordsStr.length
// let minWordLength = words[0].length
// // words.forEach(i => { // 可以改为支持不同长度word的版本
// // Math.min(minWordLength, i.length)
// // })
// const strLength = s.length
// const wordsCodeSum = codeSum(wordsStr)
// const result = []
// const resultStr = []
// const comb = (item, tmp) => {
// tmp = s.indexOf(item, tmp === null ? 0 : tmp)
// if (tmp !== -1) {
// const max = wordLength - item.length
// let start = Math.max(tmp - max, 0)
// let end = tmp + item.length + max
// if (end > strLength) {
// end = strLength
// }
// let tmpStr = s.substring(start, end)
// let n = 0
// while (n < tmpStr.length - wordLength + 1) {
// const sub = tmpStr.substring(n, n + wordLength)
// const subLeft = n - minWordLength >= 0 ? tmpStr.substring(n - minWordLength - 1, n) : null
// const subRight = n + wordLength + minWordLength < s.length ? tmpStr.substring(n + wordLength, n + wordLength + minWordLength) : null
// if (subLeft && !words.includes(subLeft) && subRight && !words.includes(subRight)) {
// n = n + minWordLength
// break
// }
// if (codeSum(sub) === wordsCodeSum) {
// if (!result.includes(start + n)) { // !Set
// result.push(start + n)
// resultStr.push(sub)
// }
// }
// n = n + minWordLength
// }
// comb(item, tmp + 1)
// }
// }
// words.forEach((item) => {
// comb(item)
// })
// const res = []
// const resultStrLength = resultStr.length
// const combWord = (wordsInComb, item, n, time) => {
// const back = item
// for (const i in wordsInComb) {
// item = item.replace(wordsInComb[i], ' ')
// }
// if (item.trim() === '') {
// const t = result[n]
// if (!res.includes(t)) res.push(t)
// }
// if (time < wordsInComb.length) {
// wordsInComb.push(wordsInComb.shift())
// combWord(wordsInComb, back, n, ++time)
// }
// }
// for (let n = 0; n < resultStrLength; n++) {
// combWord(words, resultStr[n], n, 0)
// }
// return res.sort()
// }
// Copy from https://github.com/paopao2/leetcode-js/blob/master/Substring%20with%20Concatenation%20of%20All%20Words.js
export default (s: string, words: string[]): number[] => {
if (s === '' || words.length === 0) return []
let len = s.length
let wordsLen = words.length
let wordLen = words[0].length
let i
let j
let m
let temp: string
const toFound: any = {}
let result: number[] = []
for (i = 0; i < wordsLen; i++) {
if (!toFound[words[i]]) {
toFound[words[i]] = 1
} else {
toFound[words[i]]++
}
}
for (i = 0; i < len; i++) {
let found: any = {}
j = i
for (m = 0; m < wordsLen; m++) {
temp = s.slice(j, j + wordLen)
if (!toFound[temp]) {
break
}
if (toFound[temp]) {
if (!found[temp]) {
found[temp] = 1
} else {
found[temp]++
}
}
if (found[temp] > toFound[temp]) {
break
}
j += wordLen
}
if (m === wordsLen) {
result.push(i)
}
}
return result
// const wordsStr = words.join('')
// const wordArrSort = wordsStr.split('').sort().join('')
// const wordsLength = wordsStr.length
// const wordLength = words[0].length // 可以改为支持不同长度word的版本
// const strLength = s.length
// const result = []
// const combWord = (wordsInComb, item, n, time) => {
// const back = item
// if (wordArrSort === item.split('').sort().join('')) {
// for (const i in wordsInComb) {
// item = item.replace(wordsInComb[i], ' ')
// // console.log(item)
// if (!item) {
// break
// }
// }
// if (item.trim() === '') {
// if (!result.includes(n)) result.push(n)
// }
// }
// if (time < wordsInComb.length) {
// wordsInComb.push(wordsInComb.shift())
// combWord(wordsInComb, back, n, ++time)
// }
// }
// const comb = (item, tmp) => {
// tmp = s.indexOf(item, tmp === null ? 0 : tmp)
// if (tmp !== -1) {
// const max = wordsLength - wordLength
// let start = Math.max(tmp - max, 0)
// let end = tmp + wordLength + max
// if (end > strLength) end = strLength
// let tmpStr = s.substring(start, end)
// let n = 0
// while (n < tmpStr.length - wordsLength + 1) {
// const sub = tmpStr.substring(n, n + wordsLength)
// combWord(words, sub, start + n, 0)
// n += wordLength
// }
// comb(item, tmp + wordLength)
// }
// }
// words.forEach((item) => {
// comb(item)
// })
// return result.sort()
}