Skip to content

Commit ac8a3cf

Browse files
committed
Create 1371.每个元音包含偶数次的最长子字符串.js
1 parent 49bb2ec commit ac8a3cf

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var findTheLongestSubstring = function(s) {
6+
const lst = new Array(32).fill(-1);
7+
let result = 0;
8+
let h = 0;
9+
lst[h] = 0;
10+
for (let i = 0; i < s.length; i++) {
11+
if (s[i] === 'a') h ^= (1 << 0);
12+
else if (s[i] === 'e') h ^= (1 << 1);
13+
else if (s[i] === 'i') h ^= (1 << 2);
14+
else if (s[i] === 'o') h ^= (1 << 3);
15+
else if (s[i] === 'u') h ^= (1 << 4);
16+
if (lst[h] >= 0) {
17+
result = Math.max(result, i + 1 - lst[h]);
18+
} else {
19+
lst[h] = i + 1;
20+
}
21+
}
22+
return result;
23+
};

0 commit comments

Comments
 (0)