-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path30.zy445566.js
69 lines (69 loc) · 2.5 KB
/
30.zy445566.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
/**
* @param {string} s
* @param {string[]} words
* @return {number[]}
*/
var findSubstring = function(s, words) {
if (words.length==0){return [];}
if (s.length==0) {return [];}
if (words.length>s.length){return [];}
if (s.length>=10000) {return [];}
let wordsMap = {};
let indexMap = {}
let indexList = [];
for(let i=0;i<words.length;i++) {
let startIndex = 0
wordsMap[i] = [];
let wordsIndex=-1;
do {
wordsIndex = s.indexOf(words[i],startIndex);
if (wordsIndex>=0) {
if (indexMap[wordsIndex]!=undefined) {
if(indexMap[wordsIndex].index.indexOf(i)<0){
indexMap[wordsIndex].index.push(i)
}
} else {
indexMap[wordsIndex] = {words:words[i],index:[i]};
}
wordsMap[i].push({wordsIndex:wordsIndex,words:words[i],index:i});
startIndex = wordsIndex+1;
}
}while (wordsIndex>=0);
}
if (words.length==1) {
for(let resIndex of wordsMap[0]) {
indexList.push(resIndex.wordsIndex);
}
return indexList;
}
for(let wordIndex in wordsMap) {
for (let i=0;i<wordsMap[wordIndex].length;i++) {
let wordsCount = 1;
let indexInc = wordsMap[wordIndex][i].wordsIndex+wordsMap[wordIndex][i].words.length;
let existList = [wordsMap[wordIndex][i].index];
while (wordsCount<words.length) {
if(indexMap[indexInc]!=undefined) {
let isExistValue = -1;
for(let repeatIndexValue of indexMap[indexInc].index) {
if (existList.indexOf(repeatIndexValue)<0){
isExistValue = repeatIndexValue;
}
}
if (existList.indexOf(isExistValue)<0 && isExistValue>-1) {
wordsCount++;
existList.push(isExistValue);
if (wordsCount==words.length && indexList.indexOf(wordsMap[wordIndex][i].wordsIndex)<0) {
indexList.push(wordsMap[wordIndex][i].wordsIndex)
}
indexInc+=indexMap[indexInc].words.length;
} else {
break;
}
} else {
break;
}
}
}
}
return indexList;
};