-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.ts
32 lines (32 loc) · 874 Bytes
/
Solution.ts
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
function maxSubstringLength(s: string): number {
const first: number[] = Array(26).fill(-1);
const last: number[] = Array(26).fill(0);
const n = s.length;
for (let i = 0; i < n; ++i) {
const j = s.charCodeAt(i) - 97;
if (first[j] === -1) {
first[j] = i;
}
last[j] = i;
}
let ans = -1;
for (let k = 0; k < 26; ++k) {
const i = first[k];
if (i === -1) {
continue;
}
let mx = last[k];
for (let j = i; j < n; ++j) {
const a = first[s.charCodeAt(j) - 97];
if (a < i) {
break;
}
const b = last[s.charCodeAt(j) - 97];
mx = Math.max(mx, b);
if (mx === j && j - i + 1 < n) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}