Find the Longest Substring Containing Vowels in Even Counts LeetCode solution in Java

1
class Solution {
2

3
// taken help as for my code TLE T.C = (n^2);
4

5
/* static boolean check(String s){
6
int n = s.length();
7

8
int arr[] = new int[26];
9

10
for(int i=0; i<n; i++){
11
int a = s.charAt(i)-97;
12
arr[a]++;
13
}
14

15
if(arr[0]%2!=0 || arr[4]%2!=0 || arr[8]%2!=0 || arr[14]%2!=0 ||arr[20]%2!=0) return false;
16
else return true;
17
}
18

19
static int helper(String str){
20
int ans = 0;
21
int n = str.length();
22
StringBuilder s = new StringBuilder(str);
23

24
for(int k=1; k<=n; k++){
25
for(int i=0; i<=n-k; i++){
26
String x = s.substring(i,i+k);
27
if(check(x)){
28
ans = Math.max(ans,k);
29
break;
30
}
31
}
32
}
33
return ans;
34
}
35

36
*/
37
public int findTheLongestSubstring(String s) {
38

39
int[] map = new int[32];
40
Arrays.fill(map, -2);
41
map[0] = -1;
42
int n = s.length(), mask = 0, len = 0;
43
for (int i = 0; i < n; i++) {
44
char ch = s.charAt(i);
45
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
46
mask ^= 1 << (4 - (ch == 'a' ? 0 : ch == 'e' ? 1 : ch == 'i' ? 2 : ch == 'o' ? 3 : 4));
47
if (map[mask] == -2) map[mask] = i;
48
else len = Math.max(len, i - map[mask]);
49
}
50
return len;
51
}
52
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0