Skip to content

Commit e89a690

Browse files
authored
Update 1371-find-the-longest-substring-containing-vowels-in-even-counts.js
1 parent 0419215 commit e89a690

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

1371-find-the-longest-substring-containing-vowels-in-even-counts.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var findTheLongestSubstring = function(s) {
6+
const hash = {0: -1};
7+
const vowels = 'aeiou';
8+
const vowelsSet = new Set(vowels);
9+
const n = s.length;
10+
const key = (ch) => {
11+
const idx = vowels.indexOf(ch);
12+
return idx === -1 ? 0 : 1 << idx;
13+
}
14+
let state = 0;
15+
let res = 0;
16+
for(let i = 0; i < n; i++) {
17+
const ch = s[i]
18+
let tmp = state;
19+
if(vowelsSet.has(ch)) {
20+
tmp ^= key(ch);
21+
}
22+
if(hash[tmp] === undefined) {
23+
hash[tmp] = i;
24+
}else {
25+
res = Math.max(res, i - hash[tmp]);
26+
}
27+
28+
state = tmp
29+
}
30+
31+
return res
32+
};
33+
34+
// another
35+
136
/**
237
* @param {string} s
338
* @return {number}

0 commit comments

Comments
 (0)