iq.lab
Python starts when a code cell comes near or you run one
mediumSliding windowHash maps and sets target 25 min

Longest stretch without a repeat

A substring is a block of consecutive characters from a string: "ana" is a substring of "banana", but "bnn" is not.

Return the length of the longest substring of s whose characters are all different. Every character counts, spaces included, and an upper case letter is a different character from its lower case form. An empty string has no characters, so its answer is 0.

Example 1
Inputs = "abcbde"Output4

"cbde" has four different characters. Any longer block would hold both b's.

Example 2
Inputs = "banana"Output3

"ban" works. Every block of 4 letters repeats an a or an n.

Example 3
Inputs = "zzzz"Output1

Every block of 2 is "zz", so the best is a single z.

Constraints
  • 0 ≤ len(s) ≤ 105

  • s can hold any characters: letters, digits, spaces and symbols.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.