Skip to content
AI360Xpert
Beta
Difficulty: MediumSliding Window

Longest Substring Without Repeating Characters

Problem in Plain English

Given a string s, return the length of the longest contiguous substring in which every character appears exactly once.

Problem Statement

Given a string s, return the length of its longest contiguous substring with no repeated character. Characters next to each other in the answer must occupy adjacent positions in s; skipping a repeated character would create a subsequence, not a substring.

Return 0 for an empty input. Letter case, digits, punctuation, and spaces all participate in the same uniqueness rule.

Constraints

  • 0 <= s.length <= 10^5
  • s consists of English letters, digits, symbols and spaces.

Examples

Example 1

Input
s = "abcabcbb"
Output
3

The longest substring without repeating characters is "abc", with length 3.

Example 2

Input
s = "bbbbb"
Output
1

Every longer window repeats "b", so the answer is "b" with length 1.

Example 3

Input
s = "pwwkew"
Output
3

The longest substring without repeating characters is "wke", with length 3.

Example 4

Input
s = ""
Output
0

There is no non-empty substring, so the maximum length is zero.

Example 5

Input
s = "abba"
Output
2

The repeated b forces removal of both a and the earlier b. The longest valid windows are ab and ba, each length two.

Example 6

Input
s = "a A1!"
Output
5

Lowercase a, a space, uppercase A, digit 1, and ! are five distinct characters. Spaces and case are significant.

Intuition

Keep a window of consecutive characters and remember exactly which characters it contains. In abcabcbb, the first abc is unique, but the next a repeats the window's a. Dropping that earlier a leaves bca, which is still a length-three candidate. Remove only enough of the left side to eliminate a repeat; preserving the remaining characters lets later windows grow without restarting.

Approaches

Brute Force

Solution Details

Reveal Brute Force: intuition, complexity, and code

Hints

Hint 1
Fix a start index and grow the window to the right until a character repeats: everything before the repeat is a valid substring.
Hint 2
When the right edge hits a duplicate, the window can only become valid again by dropping characters from the left, one at a time, until the duplicate is gone.
Hint 3
A hash set holding exactly the current window makes both the duplicate test and the left-edge removals constant time.

Edge Cases

  • The empty string returns 0 without entering either scan.
  • A uniform string such as bbbbb returns 1; a fully distinct string such as abcdef returns its full length 6.
  • Repeated characters separated by others may require several left removals: abba returns 2, not 3 or 4.
  • Spaces, punctuation, digits, and case are significant: a space repeated twice is a duplicate, while a and A differ.
  • Substring continuity matters: pwwkew has answer 3; selecting pwke skips a position and is not a valid answer.
  • For the stated English-character inputs the three implementations agree. If extended to arbitrary supplementary Unicode, Java/JavaScript indexing uses UTF-16 code units while Python uses code points; choose consistent character units before reusing the snippets.

Common Mistakes and Interview Tips

  • Using if instead of while for duplicate removal may leave the duplicate inside the window after only one removal.
  • Clearing the whole set when a repeat arrives loses useful suffix candidates; remove only through the earlier occurrence.
  • Adding the incoming character before shrinking destroys the exact relationship between the set and the current window. Shrink first, then insert.
  • Computing length as right - left instead of right - left + 1 drops one character.
  • Interview tip: explain why the set matches the current unique window, and why both pointers move only forward. A nested loop is still linear here because each left removal happens at most once per position.

Key Takeaway

A sliding window plus a set turns uniqueness constraints into a one-pass scan: expand until the rule breaks, then shrink from the left just enough to restore it.