forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
35 lines (35 loc) · 992 Bytes
/
Solution.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
class Solution {
public int maxSubstringLength(String s) {
int[] first = new int[26];
int[] last = new int[26];
Arrays.fill(first, -1);
int n = s.length();
for (int i = 0; i < n; ++i) {
int j = s.charAt(i) - 'a';
if (first[j] == -1) {
first[j] = i;
}
last[j] = i;
}
int ans = -1;
for (int k = 0; k < 26; ++k) {
int i = first[k];
if (i == -1) {
continue;
}
int mx = last[k];
for (int j = i; j < n; ++j) {
int a = first[s.charAt(j) - 'a'];
int b = last[s.charAt(j) - 'a'];
if (a < i) {
break;
}
mx = Math.max(mx, b);
if (mx == j && j - i + 1 < n) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
}