Longest Substring Without Repeating Characters
Given a string s, find the length of the longest substring without repeating characters.
Solution
Use two pointers to keep track of the current substring. Use a map to keep track of the last seen index of each character. If the current character is already in the map and the last seen index of the character is greater than or equal to the left
pointer, move the left
pointer to the last seen index of the character + 1. Update the last seen index of the character. Update the max length of the substring.
Implementation
Pseudocode
- Create a map to keep track of the last seen index of each character.
- Create two pointers,
left
andright
to keep track of the current substring. - Loop through the string from left to right.
- If the current character is already in the map and the last seen index of the character is greater than or equal to the
left
pointer, move theleft
pointer to the last seen index of the character + 1. - Update the last seen index of the character.
- Update the max length of the substring.
- If the current character is already in the map and the last seen index of the character is greater than or equal to the
- Return the max length of the substring.