/
LongestSubstringWithAtleastKRepeatingCharacters.java
63 lines (56 loc) · 1.93 KB
/
LongestSubstringWithAtleastKRepeatingCharacters.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/
class Solution {
public int longestSubstring(String s, int k) {
int[] freq = new int[26];
char[] arr = s.toCharArray();
int maxFreq = 0, max = 0;
for (char c: arr) {
freq[c - 'a']++;
maxFreq = Math.max(maxFreq, freq[c - 'a']);
}
if (maxFreq < k) return 0;
int[] tempFreq = new int[26];
int start = -1;
boolean isRecording = false;
for (int i = 0; i < arr.length; i++) {
char c = arr[i];
if (freq[c - 'a'] >= k) {
if (!isRecording) {
isRecording = true;
start = i;
}
tempFreq[c - 'a']++;
}
if (freq[c - 'a'] < k || i == arr.length - 1) {
if (!isRecording) continue;
int end = i - (freq[c - 'a'] >= k ? 0 : 1);
isRecording = false;
// Check for validity of tempFreq
boolean isValid = true;
boolean partiallyValid = false;
for (int x: tempFreq) {
if (x != 0 && x < k) {
isValid = false;
} else if (x != 0 && x >= k) {
partiallyValid = true;
}
}
if (isValid) {
int len = end - start + 1;
if (len > max) {
max = len;
}
} else if (partiallyValid) {
int len = longestSubstring(s.substring(start, end + 1), k);
if (len > max) {
max = len;
}
}
for (int j = 0; j < 26; j++) {
tempFreq[j] = 0;
}
}
}
return max;
}
}