The intuition behind this solution is to use a sliding window approach. We maintain a window of characters that contains no duplicates and expand or contract this window as we move through the string.
The approach can be described as follows:
- Initialize variables:
maxLength
: To keep track of the longest substring length.foundCharacters
: AHashSet
to store unique characters in the current window.leftIndex
andrightIndex
: Pointers defining the current window.
- Slide the window through the string:
- Expand the window by moving
rightIndex
:- If the character at
rightIndex
is not infoundCharacters
, add it and moverightIndex
. - If it is in
foundCharacters
, remove the character atleftIndex
and moveleftIndex
.
- If the character at
- Expand the window by moving
- Update
maxLength
: After each iteration, updatemaxLength
if the current window is longer. - Continue until either pointer reaches the end of the string.
- Return
maxLength
as the result.
- Time Complexity:
O(n)
wheren
is the length of the input string. Each character is processed at most twice (once byrightIndex
and once byleftIndex
).HashSet
operations (add
,remove
,contains
) areO(1)
on average. - Space Complexity:
O(n)
In the worst case, the HashSet stores all unique characters in the string.
This solution efficiently finds the longest substring without repeating characters using a sliding window technique.
Key points:
- The sliding window approach allows us to solve the problem in a single pass through the string.
- Using a
HashSet
forfoundCharacters
providesO(1)
lookup, insertion, and deletion. - The solution handles edge cases well, including empty strings and strings with all unique characters.
Strengths:
- Efficient time complexity of
O(n)
. - Space-efficient, using only additional space proportional to the unique characters.
- The code is concise and readable.
Potential improvements:
For very large character sets, we should consider using a boolean array instead of a HashSet
if the character range
is known and limited. The solution could be optimized slightly by moving leftIndex
directly to the position after the
first occurrence of the duplicate character, rather than incrementing by one each time.