Problem · String
Longest Substring Without Repeating Characters
Learn this problemProblem statement
You are given a string s consisting of printable ASCII characters.
Return the length of the longest substring of s that contains no repeated character.
The empty string has length 0.
Function
lengthOfLongestSubstring(s: String) → intExamples
Example 1
s = "abcabcbb"return = 3The substring abc has length 3 and no repeated character. Longer windows such as abca repeat a.
Example 2
s = "bbbbb"return = 1Every character is b, so the longest non-repeating substring has length 1.
Example 3
s = "pwwkew"return = 3The substring wke has length 3. The answer must be a contiguous substring, so pwke is not valid.
Constraints
0 <= s.length <= 5 * 10^4.scontains only printable ASCII characters.