Skip to content

Commit 89f1925

Browse files
committed
Add solution #1371
1 parent 8374ddd commit 89f1925

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,259 LeetCode solutions in JavaScript
1+
# 1,260 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1043,6 +1043,7 @@
10431043
1367|[Linked List in Binary Tree](./solutions/1367-linked-list-in-binary-tree.js)|Medium|
10441044
1368|[Minimum Cost to Make at Least One Valid Path in a Grid](./solutions/1368-minimum-cost-to-make-at-least-one-valid-path-in-a-grid.js)|Hard|
10451045
1370|[Increasing Decreasing String](./solutions/1370-increasing-decreasing-string.js)|Easy|
1046+
1371|[Find the Longest Substring Containing Vowels in Even Counts](./solutions/1371-find-the-longest-substring-containing-vowels-in-even-counts.js)|Medium|
10461047
1372|[Longest ZigZag Path in a Binary Tree](./solutions/1372-longest-zigzag-path-in-a-binary-tree.js)|Medium|
10471048
1374|[Generate a String With Characters That Have Odd Counts](./solutions/1374-generate-a-string-with-characters-that-have-odd-counts.js)|Easy|
10481049
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1371. Find the Longest Substring Containing Vowels in Even Counts
3+
* https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/
4+
* Difficulty: Medium
5+
*
6+
* Given the string s, return the size of the longest substring containing each vowel an even number
7+
* of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.
8+
*/
9+
10+
/**
11+
* @param {string} s
12+
* @return {number}
13+
*/
14+
var findTheLongestSubstring = function(s) {
15+
const vowelMap = new Map([['a', 0], ['e', 1], ['i', 2], ['o', 3], ['u', 4]]);
16+
const stateToIndex = new Map([[0, -1]]);
17+
let state = 0;
18+
let result = 0;
19+
20+
for (let i = 0; i < s.length; i++) {
21+
if (vowelMap.has(s[i])) {
22+
state ^= 1 << vowelMap.get(s[i]);
23+
}
24+
if (stateToIndex.has(state)) {
25+
result = Math.max(result, i - stateToIndex.get(state));
26+
} else {
27+
stateToIndex.set(state, i);
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)