You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
817 B
TypeScript
33 lines
817 B
TypeScript
export function maxLengthSubString(str: string): number {
|
|
const endIndex = str.length - 1;
|
|
let res = 1;
|
|
let L = 0;
|
|
let R = 1;
|
|
let subStr = str[L];
|
|
|
|
if (!str) return 0;
|
|
if (str.length < 2) return 1;
|
|
|
|
while (L <= endIndex && R <= endIndex) {
|
|
if (!subStr.includes(str[R])) {
|
|
subStr = subStr + str[R];
|
|
res = Math.max(subStr.length, res);
|
|
R++;
|
|
} else {
|
|
L++;
|
|
subStr = str[L];
|
|
R = L + 1;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
console.log(maxLengthSubString("a"), "expect: " + 1);
|
|
console.log(maxLengthSubString(""), "expect: " + 0);
|
|
|
|
console.log(maxLengthSubString("abcabcbb"), "expect: " + 3);
|
|
console.log(maxLengthSubString("ab"), "expect: " + 2);
|
|
console.log(maxLengthSubString("aaaaaa"), "expect: " + 1);
|
|
console.log(maxLengthSubString("aabcDfffaaaa"), "expect: " + 5);
|