Longest substring without repeats

Find the length of the longest substring (a contiguous part) that has no repeated character.

Input

One line of up to 10⁵ characters (letters, digits, spaces and symbols); it may be empty.

Output

The length.

Examples

Example 1 · Input
abcabcbb
Expected output
3
Example 2 · Input
bbbbb
Expected output
1

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

A window from left to right, and a set of the characters inside it.

Hint 2

When the next character is already inside, shrink from the left until it is not.

Hint 3

Track the largest window size.

Solution

Show one correct solution (try it yourself first)
s = input()
inside = set()
left = best = 0
for right, ch in enumerate(s):
    while ch in inside:
        inside.remove(s[left])
        left += 1
    inside.add(ch)
    best = max(best, right - left + 1)
print(best)

Lesson: Sliding windows →