8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 531104a commit 30d8940Copy full SHA for 30d8940
December 2024 Leetcode Solution/December-10.java
@@ -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