Back
Strings & Hashing (Medium) / 171. Longest Substring Without Repeating Characters
00:00
1/6
171. Longest Substring Without Repeating Characters
MediumGiven a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
- 1
Sliding Window: Use a window defined by
leftandrightpointers. - 2
Tracking: Use a hash map (or dictionary) to store the last seen index of each character.
- 3
Logic:
- 4
Iterate with
right. - 5
If
s[right]is in map and its index is >=left, it means we found a repeat inside the current window. - 6
Move
lefttomap[s[right]] + 1. - 7
Update
map[s[right]] = right. - 8
Update
max_len = max(max_len, right - left + 1).
PYTHON PLAYGROUND
PYTHON PLAYGROUND
⏳ Loading editor…