-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
65 lines (62 loc) · 1.48 KB
/
Copy pathSolution.java
File metadata and controls
65 lines (62 loc) · 1.48 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution
{
public int findTheLongestSubstring(String s)
{
final int n = s.length();
final char[] ss = s.toCharArray();
final int[][] count = new int[n][5];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5 && i > 0; j++)
{
count[i][j] = count[i - 1][j];
}
int index = _getVowelIndex(ss[i]);
if (index >= 0)
{
count[i][index]++;
}
}
for (int i = n; i > 0; i--)
{
for (int j = 0; j < n && i + j - 1 < n; j++)
{
if (_isEven(j, i + j - 1, count))
{
return i;
}
}
}
return 0;
}
private boolean _isEven(int start, int end, final int[][] count)
{
for (int i = 0; i < 5; i++)
{
int charCount = count[end][i] - (start > 0 ? count[start - 1][i] : 0);
if (charCount % 2 != 0)
{
return false;
}
}
return true;
}
private int _getVowelIndex(char ssChar)
{
switch (ssChar)
{
case 'a':
return 0;
case 'e':
return 1;
case 'i':
return 2;
case 'o':
return 3;
case 'u':
return 4;
default:
return -1;
}
}
}