Description:
Given a string str, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbbaa"
Output: 3
Explanation: The longest substring is "abc".
Example 2:
Input: s = "aaaaaaa"
Output: 1
Explanation: The longest substring is "a".
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The longest substring is "wke".
This problem is efficiently solved using the sliding window technique with a Set to track unique characters in the current window.
- Initialize an empty Set to store unique characters.
- Use two pointers,
leftandright, to represent the current window. - Move the
rightpointer through the string:- If the character at
rightis not in the Set, add it and update the maximum length. - If the character is already in the Set, remove characters from the left of the window (increment
leftand remove from Set) until the duplicate is removed.
- If the character at
- Continue until the end of the string.
- The maximum window size found is the answer.
Time and Space complexity:
- Time Complexity: O(n), where n is the length of the string (each character is visited at most twice).
- Space Complexity: O(min(n, m)), where n is the length of the input string andm is the size of the character set (e.g., 26 for lowercase English letters).
