easy +10 pts

Longest Substring Without Repeating Characters

Find the length of the longest substring without repeating characters using an efficient sliding window.

Write a function **`length_of_longest_substring(s: str) -> int`** that takes a string `s` and returns the length of the longest substring without repeating characters. A substring is a contiguous sequence of characters within the string. For example, in `"abcabcbb"`, the longest substring without repeating characters is `"abc"` (length 3). Your solution must run in O(n) time and O(min(n, alphabet_size)) space.

Constraints

Input string length: 0 ≤ len(s) ≤ 10^5. Characters are any printable ASCII characters (32–126). The solution should be efficient enough for the maximum length.

Example

>>> length_of_longest_substring("abcabcbb")
3
>>> length_of_longest_substring("bbbbb")
1
>>> length_of_longest_substring("pwwkew")
3
>>> length_of_longest_substring("")
0
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use two pointers (left and right) to maintain a valid window of unique characters.
Store the last seen index of each character in a dictionary to move the left pointer efficiently.
When a repeat is found, update the left pointer to one past the previous occurrence, then continue expanding the right pointer.
Track the maximum window size encountered at each step.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.