forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1371.java
47 lines (43 loc) · 1.35 KB
/
_1371.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.Map;
public class _1371 {
public static class Solution1 {
public int findTheLongestSubstring(String s) {
int max = 0;
for (int i = 0; i < s.length(); i++) {
Map<Character, Integer> map = setupMap();
if (s.length() - i < max) {
return max;
}
for (int j = i; j < s.length(); j++) {
Character b = s.charAt(j);
if (map.containsKey(b)) {
map.put(b, map.get(b) + 1);
}
if (allEven(map)) {
max = Math.max(max, j - i + 1);
}
}
}
return max;
}
private Map<Character, Integer> setupMap() {
Map<Character, Integer> map = new HashMap<>();
map.put('a', 0);
map.put('e', 0);
map.put('i', 0);
map.put('o', 0);
map.put('u', 0);
return map;
}
private boolean allEven(Map<Character, Integer> map) {
for (char c : map.keySet()) {
if (map.get(c) % 2 != 0) {
return false;
}
}
return true;
}
}
}