Longest Substring Without Repeating Characters
Problem statement
Given a string s, return the length of the longest substring of s in which every character is different.
A substring is a run of consecutive characters. Characters are compared exactly: A and a are different, and spaces, digits, and symbols are characters too.
If s is empty, return 0.
Function
lengthOfLongestSubstring(s: String) → intExamples
Example 1
s = "abcbdeb"return = 4The substring "cbde" has 4 different characters. Every substring of length 5 contains b twice, so the answer is 4.
Example 2
s = "aaaa"return = 1Any substring longer than one character repeats a, so the answer is 1.
Example 3
s = "Hi, hi!"return = 5H and h are different characters, and the comma and the space count as characters too. "Hi, h" has 5 different characters. Both substrings of length 6 contain i twice, so the answer is 5.
Constraints
0 <= s.length <= 5 * 10^4scontains only printable ASCII characters (codes 32 to 126): English letters, digits, spaces, and symbols.