-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLongestSubstringKRepeatingCharacters.java
More file actions
66 lines (41 loc) · 1.39 KB
/
Copy pathLongestSubstringKRepeatingCharacters.java
File metadata and controls
66 lines (41 loc) · 1.39 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
66
import java.util.*;
public class LongestSubstringKRepeatingCharacters{
/**leetcode : https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
* implementation
*/
public int longestSubstring(String s, int k) {
int n = s.length();
if (n == 0 || n < k) {
return 0;
}
if (k <=1 ) {
return n;
}
Map<Character, Integer> freq = new HashMap<>();
//O(n)
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
freq.put(c, freq.getOrDefault(c, 0) + 1);
}
int right = 0;
int output = 0;
while(right < n && freq.get(s.charAt(right)) >= k) {
right++;
}
if (right >= n -1){
return right;
}
int ls1 = longestSubstring(s.substring(0, right), k);
while(right < n && freq.get(s.charAt(right)) < k ) {
right++;
}
int ls2 = right < n ? longestSubstring(s.substring(right), k) : 0;
return Math.max(ls1, ls2);
}
public static void main(String[] args) {
LongestSubstringKRepeatingCharacters ls = new LongestSubstringKRepeatingCharacters();
assert ls.longestSubstring("aaabbb", 3) == 6;
assert ls.longestSubstring("ababacb", 3) == 0;
assert ls.longestSubstring("bbaaacbd", 3) == 3;
}
}