8000 Create December-10.java · Hunterdii/Leetcode-POTD@30d8940 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30d8940

Browse files
authored
Create December-10.java
1 parent 531104a commit 30d8940

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int maximumLength(String s) {
3+
int n = s.length(), l = 0, r = n;
4+
while (l < r) {
5+
int mid = (l + r + 1) >> 1;
6+
if (isValid(s, n, mid)) l = mid;
7+
else r = mid - 1;
8+
}
9+
return l == 0 ? -1 : l;
10+
}
11+
12+
private boolean isValid(String s, int n, int x) {
13+
int[] freq = new int[26];
14+
for (int i = 0; i < n;) {
15+
int j = i + 1;
16+
while (j < n && s.charAt(j) == s.charAt(i)) j++;
17+
freq[s.charAt(i) - 'a'] += Math.max(0, j - i - x + 1);
18+
if (freq[s.charAt(i) - 'a'] >= 3) return true;
19+
i = j;
20+
}
21+
return false;
22+
}
23+
}

0 commit comments

Comments
 (0)
0