JavaScriptMediumStrings20 min
Find the longest substring without repeats
Return the maximum length of a contiguous substring containing no repeated ASCII character. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess keep a sliding window and jump past the previous occurrence.
Input
A JSON string on stdin.
Output
Print one compact JSON value: an integer length.
Constraints
0 <= string length <= 10000; ASCII characters only.
Example
Input
"abcabcbb"
Output
3
Example 2 input
"a b!a"
Output
4
Boundary case: Never move the left boundary backwards
Public test cases (2)
Input
"abcabcbb"
Expected output
3
Input
"bbbbb"
Expected output
1
Hints (2)
- Remember the most recent index of each character
- Never move the left boundary backwards
Target complexity: time O(n) · space O(1) for the fixed ASCII alphabet
