LeetCode / Longest Substring Without Repeating Characters
Problem
- Link
- Description
- Find the longest substring without repeating characters
- Type
- Brute Force
- Sliding Window Pattern
Solution 1
| |
- Description
- Create a map and check for duplicate characters while searching for the longest substring
- If the character is not in the map, store it along with its index
- If the character exists in the map, restart the search from the index where it was found
- Time Complexity
- O(len(s))
- Space Complexity
- O(len(s))
Solution 2
| |
- Description
- Use sliding window and store the last position of each character in a map
- When a duplicate character appears, move
left_indexto remove the duplicate - At each step, calculate
right_index - left_index + 1and compare withmax_lengthto update the maximum length
- Time Complexity
- O(len(s))
- Space Complexity
- O(len(s))