Longest Substring Without Repeating Characters

Length of the longest substring with no repeated characters

MediumSliding WindowHash MapString

Find the length of the longest substring without repeated characters.

Examples

Example 1

Input: s = "abcabcbb"
Output: 3

Example 2

Input: s = "bbbbb"
Output: 1

Constraints

  • 1 ≤ |s| ≤ 5 * 10⁴
  • s consists of ASCII characters.

Notes

  • Use a sliding window and remember the last seen index of each character.
  • When you encounter a duplicate, shrink the window from the left.

Hints

Show hint 1

Maintain a dictionary mapping character -> last seen index.

Show hint 2

Track the current window start; jump it past duplicates instead of removing one char at a time.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • How would this change if we needed the substring itself, not just its length?
  • Can you support Unicode grapheme clusters efficiently?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem