Longest Substring Without Repeating Characters

mediumSliding Windowsliding window + hash setB75Solve on LeetCode#3

Part of Practice: Longest Valid Windows

Given a string s, find the length of the longest substring that contains no repeated character. A substring is a contiguous run of characters — "abc" is a substring of "abcde", but "abd" is not.

The string can hold letters, digits, symbols, and spaces, and can be up to about 5 * 10^4 characters long, so checking every substring is too slow. The empty string is allowed and answers 0.

Examples

Input:
s = "abcabcbb"
Output:
3

The longest run with no repeats is "abc", length 3. "abca" fails because it has two a's.

Input:
s = "bbbbb"
Output:
1

Every character is b, so the best you can do is a single "b".

Input:
s = "pwwkew"
Output:
3

"wke" has length 3. "pwke" is not valid because it is a subsequence (skips a w), not a substring.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.