Skip to content

Commit bda7e5e

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

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@ var findTheLongestSubstring = function (s, V = 'aeiou', max = 0) {
1818
}
1919
return max
2020
}
21+
22+
// another
23+
24+
/**
25+
* @param {string} s
26+
* @return {number}
27+
*/
28+
const findTheLongestSubstring = function(s) {
29+
const n = s.length
30+
const ch2num = ch => {
31+
const idx = 'aeiou'.indexOf(ch)
32+
return idx === -1 ? 0 : (1 << idx)
33+
}
34+
let res = 0
35+
const arr = Array(n + 1).fill(0)
36+
const hash = new Map([[0, 0]])
37+
for(let i = 1; i <= n; i++) {
38+
arr[i] = arr[i - 1] ^ ch2num(s[i - 1])
39+
const first = hash.has(arr[i]) ? hash.get(arr[i]) : i
40+
if (!hash.has(arr[i])) hash.set(arr[i], i)
41+
res = Math.max(res, i - first)
42+
}
43+
44+
return res
45+
};

0 commit comments

Comments
 (0)