Given a string, find the length of the longest substring without repeating characters.
For example, given s = "abcabcbb", the answer is 3 because "abc" is the longest substring without repeating characters. For s = "bbbbb", the answer is 1. For s = "pwwkew", the answer is 3 ("wke").
Explain your approach, how you detect and handle repeated characters, and analyze the time and space complexity.
How to approach it
- Hint 1
Use a window that expands to the right. When you encounter a repeated character, you need to shrink the window.
- Hint 2
A hash set can track which characters are currently in the window.
- Hint 3
Instead of shrinking one character at a time, a hash map storing the last index of each character lets you jump the left pointer directly past the duplicate.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.