-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
39 lines (37 loc) · 1017 Bytes
/
Copy pathSolution.java
File metadata and controls
39 lines (37 loc) · 1017 Bytes
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
public class Solution
{
public int longestSubstring(String s, int k)
{
char[] str = s.toCharArray();
return getSubstring(str, k, 0, s.length());
}
private int getSubstring(char[] s, int k, int start, int end)
{
if (end - start < k)
{
return 0;
}
int[] counts = new int[26];
for (int i = start; i < end; i++)
{
counts[s[i] - 'a']++;
}
for (int i = 0; i < 26; i++)
{
int count = counts[i];
if (count > 0 && count < k)
{
for (int idx = start; idx < end; idx++)
{
if (s[idx] - 'a' == i)
{
int left = getSubstring(s, k, start, idx);
int right = getSubstring(s, k, idx + 1, end);
return Math.max(left, right);
}
}
}
}
return end - start;
}
}