Longest Substring Without Repeating Characters
Updated
Problem statement
Given a string s, return the length of the longest substring of s in which no character appears more than once.
A substring is a run of characters that are next to each other in s. Uppercase and lowercase letters are different characters, and a space is a character too. If s is empty, return 0.
Function
lengthOfLongestSubstring(s: String) → intExamples
Example 1
s = "xyzxyzyy"return = 3The substring "xyz" has three different characters. Any substring of length 4 repeats a character; for example "xyzx" has x twice. So the answer is 3.
Example 2
s = "aaaa"return = 1Every substring longer than one character repeats a, so the answer is 1.
Example 3
s = "dvdf"return = 3The substring "vdf" has three different characters, and the whole string repeats d, so the answer is 3. Note that the best substring starts at the v, not at the second d.
Constraints
0 <= s.length <= 5 * 10^4sconsists of English letters, digits, symbols and spaces (printable ASCII characters).