Longest Substring Without Repeating Characters
Problem statement
Given a string s, return the length of its longest contiguous substring whose characters are all distinct.
If s is empty, return 0.
Interview extension
After solving the judged task, explain how to return one longest valid substring itself by tracking the start index whenever the maximum length improves.
Function
lengthOfLongestSubstring(s: String) → intExamples
Example 1
s = "abcabcbb"return = 3"abc" is one longest substring whose characters are all distinct.
Example 2
s = "bbbbb"return = 1Every valid substring contains only one b, so the maximum length is 1.
Example 3
s = ""return = 0An empty string has no nonempty substring, so the result is 0.
Constraints
0 <= s.length <= 100000scontains printable ASCII characters.- The solution should run in
O(s.length)time.