1078-findOcurrences.js
1.06 KB
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
/**
* @param {string} text
* @param {string} first
* @param {string} second
* @return {string[]}
*/
var findOcurrences2 = function(text, first, second) {
let target = `${first} ${second} `;
let rs = [];
while (text) {
let i = text.indexOf(target);
if (i >= 0) {
console.info(text, i);
text = text.substr(i + target.length);
console.info('==>', text);
let m = /^(\w+)\b.*/.exec(text);
if (m) {
rs.push(m[1]);
}
} else {
break;
}
}
return rs;
};
var findOcurrences = function(text, first, second) {
let words = text.split(' ');
let rs = [];
for (let i = 0; i < words.length - 2; i++) {
if (words[i] === first && words[i+1] === second && words[i+2]) {
rs.push(words[i+2]);
i++;
}
}
return rs;
};
console.info(findOcurrences("we will we will rock you", "we", "will"));
console.info(findOcurrences("alice is a good girl she is a good student", "a", "good"));