forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
75 lines (72 loc) · 1.6 KB
/
main.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
/**
* @param {string} S
* @param {string[]} words
* @return {number}
*/
// var expressiveWords = function(S, words) {
// function valid (S, word) {
// let si = 0, ei = 0
// let sj = 0, ej = 0
// while (si < S.length && sj < word.length && S[si] === word[sj]) {
// while (ei !== S.length && S[ei] === S[si]) {
// ei++
// }
// while (ej !== word.length && word[ej] === word[sj]) {
// ej++
// }
// if (ei - si >= 3) {
// if (ei - si < ej - sj) {
// return false
// }
// } else { // < 3
// if (ei - si !== ej - sj) {
// return false
// }
// }
// si = ei
// sj = ej
// }
// return si >= S.length && sj >= word.length
// }
// let count = 0
// for (const word of words) {
// if (valid(S, word)) {
// count ++
// }
// }
// return count
// };
/**
* @param {string} S
* @param {string[]} words
* @return {number}
*/
var expressiveWords = function(S, words) {
function serial (word) {
let i = 0, j = 0
const ans = []
while (i < word.length) {
while (j < word.length && word[i] === word[j]) {
j++
}
ans.push(j - i)
i = j
}
return ans
}
function compare (a, b) {
if (a.length !== b.length) return false
for (let i = 0; i < a.length; i++) {
if (a[i] < b[i]) return false
if (a[i] < 3 && a[i] !== b[i]) return false
}
return true
}
let count = 0
const ss = serial(S)
for (const word of words) {
const sw = serial(word)
if (compare(ss, sw)) count++
}
return count
};