-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestSubstringwithAtLeastKRepeatingCharacter.java
58 lines (41 loc) · 1.31 KB
/
LongestSubstringwithAtLeastKRepeatingCharacter.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
package slidingwindows;
import utils.LeetCode;
import utils.Level;
@LeetCode(no=395 ,
title=" Longest Substring with At Least K Repeating Characters",
level= Level.MEDIUM,
url = "https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/")
public class LongestSubstringwithAtLeastKRepeatingCharacter {
public int longestSubstring(String s, int k) {
int n=s.length();
if(s.length()<k)
return 0;
boolean isPossible=false;
int oc[]= new int[26] ;
for(char c:s.toCharArray()){
oc[c-'a'] ++;
if(oc[c-'a']>=k){
isPossible =true;
}
}
if(!isPossible){
return 0;
}
int st=0;
while(st<n && oc[s.charAt(st)-'a']>=k ) {
// System.out.println(st);
st++;
}
//System.out.println(st);
if(st>=n-1)
return st;
// System.out.println("left");
int leftSide= longestSubstring(s.substring(0,st), k) ;
while(st<n && oc[s.charAt(st)-'a']<k) st++;
int rightSide= 0 ;
// System.out.println("rigt");
if(st<n)
rightSide= longestSubstring(s.substring(st), k) ;
return Integer.max(leftSide,rightSide);
}
}