noodleProblems/
Longest Substring Without Repeating Characters
#03

Longest Substring Without Repeating Characters

AlgorithmmediumHash TableStringSliding Window

Given a string s, return the length of the longest contiguous substring that contains no repeated characters.

A substring is a run of consecutive characters from s (not a subsequence). The answer is a single number — the length of the longest such run — not the substring itself.

Example cases

  • repeat resets window
    in s = "abcabcbb"
    out 3
    The longest run without a repeat is `abc`, length 3.
  • all same
    in s = "bbbbb"
    out 1
    Every character is `b`, so the longest run is a single `b`.
  • repeat in the middle
    in s = "pwwkew"
    out 3
    `wke` has length 3. `pwke` is not a substring (the characters aren't consecutive).
  • empty string
    in s = ""
    out 0
    An empty string has no characters, so the length is 0.

Constraints

  • 0 <= s.length <= 5 * 10^4
  • `s` consists of English letters, digits, symbols, and spaces.
Saved
s =
"abcabcbb"