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
Input
s = "abcbde"Output4"cbde" has four different characters. Any longer block would hold both b's.
Example 2
Input
s = "banana"Output3"ban" works. Every block of 4 letters repeats an a or an n.
Example 3
Input
s = "zzzz"Output1Every 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.
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.