Find the length of the longest contiguous substring that contains no repeated characters.
Longest Substring Without Repeating Characters
Given a string s, find the length of the longest contiguous substring whose characters are all distinct.
A substring must consist of consecutive characters from the original string. You only need to return the maximum length, not the substring itself.
Input Format
- A single string
s. - The string may contain lowercase letters, uppercase letters, digits, spaces, or other visible characters depending on the platform variant.
Output Format
- Return a single integer: the length of the longest substring of
swith all unique characters.
Constraints
0 <= s.length- The exact upper bound depends on the platform variant.
- A linear-time solution is expected for interview use.
Example 1
Input
s = "abcabcbb"
Output
3
Explanation
The longest substrings without repeating characters are "abc", "bca", and "cab". Their length is 3.
Example 2
Input
s = "bbbbb"
Output
1
Explanation
Every substring with more than one character repeats b, so the best you can do is a single character.
Show 1 more example
Example 3
Input
s = "pwwkew"
Output
3
Explanation
One longest valid substring is "wke", which has length 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.