Given a string s
, find the length of the longest substring without duplicate characters.
Examples:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring. "pwke" is a subsequence and not a substring.
Constraints:
0 <= s.length <= 5 × 10⁴
s
consists of English letters, digits, symbols, and spaces.
Hint: Generate all possible substrings, check each substring for duplicates, and update the maximum length accordingly.
The following tests were used to verify the correctness of the algorithm:
-
Standard LeetCode examples:
- Input:
'abcabcbb'
→ Output:3
- Input:
'bbbbb'
→ Output:1
- Input:
'pwwkew'
→ Output:3
- Input:
-
Digits:
- Input:
'1234567890'
→ Output:10
- Input:
-
Symbols:
- Input:
'!@#$^&*()'
→ Output:9
- Input:
-
Mixed letters and digits:
- Input:
'a1b2c3d4'
→ Output:8
- Input:
-
Mixed case sensitivity:
- Input:
'AaBbCc'
→ Output:6
- Input:
-
Whitespace:
- Input:
'a a a a'
→ Output:2
- Input:
-
Complex mixed characters:
- Input:
'ab@12! cd#'
→ Output:10
- Input:
- All results matched the expected outputs.
- Tests covered letters, digits, symbols, spaces, and mixed character sets to ensure full ASCII handling.
Accepted ✅
- Runtime: 263 ms (~5%) — slow (O(N^2) on purpose this time).
- Memory: 120 MB (~6%) — heavy (extra buffers/allocs).
I’m not optimizing this one. I wanted correctness and to practice checking all substrings.
Next problems: I’ll push for more efficient solutions (O(N) patterns, less memory).
![]() |
![]() |
![]() |
![]() |