Practice · Sliding window · 3 of 4

Longest Stretch Without a Repeat

You are given a string s. Return the length of the longest substring (a contiguous run of characters) in which no character appears more than once.

Example 1

Input
s = "abcbdea"
Output
5
cbdea has five different characters. Stretching it left would bring in a second b.

Example 2

Input
s = "zzzz"
Output
1

Example 3

Input
s = "ab ba"
Output
3
Spaces are characters too: ab and ba both work.

Constraints

  • s holds 0 to 5 × 10⁴ characters.
  • Every character is printable ASCII: letters, digits, symbols and spaces.