Longest Substring Without Repeating Characters

Medium Sliding Window AI round

s is a string of printable ASCII characters.

Look at the stretches of s: runs of characters that sit next to each other. A stretch is clean when no character appears in it more than once.

Return the length of the longest clean stretch, or 0 if s is empty.

Example 1

Input: s = "quartzq"
Output: 6

Of the stretches with no repeated character, "uartzq", from position 1 to position 6, is the longest, at 6 characters. The "q" on each end cannot both be in a stretch.

Example 2

Input: s = "mmmm"
Output: 1

Example 3

Input: s = "pqrqpstu"
Output: 6

Constraints

  • 0 ≤ s.length ≤ 10⁵
  • s holds only printable ASCII characters, from the space to ~

Watch the explanation

More Sliding Window

All Sliding Window problems