Skip to content

Commit 0c5730b

Browse files
committed
Time: 2 ms (99.13%), Space: 42.6 MB (93.68%) - LeetHub
1 parent b0522e6 commit 0c5730b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public int maximumLength(String s) {
3+
4+
5+
// TC : O(nlogn)
6+
// SC : O(1)
7+
8+
int n= s.length();
9+
10+
//check for 1 size if not possible -1:
11+
int start=1,end = n;
12+
13+
if(!isPossible(1,s)) return -1;
14+
15+
16+
int ans=0;
17+
while(start<=end){
18+
int mid = (start+end) / 2;
19+
if(isPossible(mid,s)){
20+
ans = mid;
21+
start = mid+1;
22+
}else{
23+
end=mid-1;
24+
}
25+
}
26+
return ans;
27+
}
28+
29+
public boolean isPossible(int k , String s){
30+
int[] cnt = new int[26];
31+
int end = 0,start=0;
32+
33+
while(end<s.length()){
34+
while(s.charAt(end)!=s.charAt(start)) start++;
35+
if(end-start+1 >= k){
36+
cnt[s.charAt(end)-'a']++;
37+
}
38+
if(cnt[s.charAt(end)-'a']>2) return true;
39+
40+
end++;
41+
}
42+
return false;
43+
}
44+
}

0 commit comments

Comments
 (0)