Problem intuition
- The naive version grows a substring from every starting point until a repeat appears.
- The optimal solution treats the substring as a sliding window and shrinks it only when a repeated character breaks validity.
Return the length of the longest substring that contains no repeated characters.
Sliding WindowThe solutions below are ordered from least optimal to most optimal, so you can see the improvement path instead of only the final answer.
Solution 1
Solution 2