Skip to content

Commit 303c648

Browse files
add 1371
1 parent d61435c commit 303c648

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1371|[Find the Longest Substring Containing Vowels in Even Counts](https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1371.java) | |Medium||String
1112
|1367|[Linked List in Binary Tree](https://leetcode.com/problems/linked-list-in-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1367.java) | |Medium||DP, Linked List, Tree
1213
|1366|[Rank Teams by Votes](https://leetcode.com/problems/rank-teams-by-votes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1366.java) | |Medium||Array, Sort
1314
|1365|[How Many Numbers Are Smaller Than the Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1365.java) | |Easy||Array, HashTable
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1371. Find the Longest Substring Containing Vowels in Even Counts
8+
*
9+
* Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.
10+
*
11+
* Example 1:
12+
* Input: s = "eleetminicoworoep"
13+
* Output: 13
14+
* Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.
15+
*
16+
* Example 2:
17+
* Input: s = "leetcodeisgreat"
18+
* Output: 5
19+
* Explanation: The longest substring is "leetc" which contains two e's.
20+
*
21+
* Example 3:
22+
* Input: s = "bcbcbc"
23+
* Output: 6
24+
* Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.
25+
*
26+
* Constraints:
27+
* 1 <= s.length <= 5 x 10^5
28+
* s contains only lowercase English letters.
29+
* */
30+
public class _1371 {
31+
public static class Solution1 {
32+
public int findTheLongestSubstring(String s) {
33+
int max = 0;
34+
for (int i = 0; i < s.length(); i++) {
35+
Map<Character, Integer> map = setupMap();
36+
if (s.length() - i < max) {
37+
return max;
38+
}
39+
for (int j = i; j < s.length(); j++) {
40+
Character b = s.charAt(j);
41+
if (map.containsKey(b)) {
42+
map.put(b, map.get(b) + 1);
43+
}
44+
if (allEven(map)) {
45+
max = Math.max(max, j - i + 1);
46+
}
47+
}
48+
}
49+
return max;
50+
}
51+
52+
private Map<Character, Integer> setupMap() {
53+
Map<Character, Integer> map = new HashMap<>();
54+
map.put('a', 0);
55+
map.put('e', 0);
56+
map.put('i', 0);
57+
map.put('o', 0);
58+
map.put('u', 0);
59+
return map;
60+
}
61+
62+
private boolean allEven(Map<Character, Integer> map) {
63+
for (char c : map.keySet()) {
64+
if (map.get(c) % 2 != 0) {
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
}
71+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1371;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1371Test {
10+
private static _1371.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1371.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(13, solution1.findTheLongestSubstring("eleetminicoworoep"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(5, solution1.findTheLongestSubstring("leetcodeisgreat"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(6, solution1.findTheLongestSubstring("bcbcbc"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(1, solution1.findTheLongestSubstring("id"));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)