Introduction
Today, we're tackling a classic string manipulation problem - finding the length of the longest substring without repeating characters. This is LeetCode Problem 3, and it's a great way to understand sliding window techniques and hash map usage in algorithms.
**The Problem
**Given a string s, find the length of the longest substring without repeating characters.
Example:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
The Optimal Solution
function lengthOfLongestSubstring(s: string): number {
let start = 0;
let maxLength = 0;
const charMap = new Map();
for (let end = 0; end < s.length; end++) {
const currentChar = s[end];
if (charMap.has(currentChar)) {
const prevIndex = charMap.get(currentChar)!;
if (prevIndex >= start) {
start = prevIndex + 1;
}
}
charMap.set(currentChar, end);
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
*This solution is:
*✅ Efficient - O(n) time complexity
✅ Space-optimized - O(min(m, n)) space (m = character set size)
✅ Clean - Uses a single pass with smart window adjustment
How It Works
Sliding Window Technique: Maintains a window of characters that haven't repeated
Hash Map Tracking: Stores the most recent index of each character
Window Adjustment: When a duplicate is found, moves the start of the window
Length Calculation: Continuously updates the maximum length found
Alternative Approaches
Brute Force Approach:
function lengthOfLongestSubstring(s: string): number {
let max = 0;
for (let i = 0; i < s.length; i++) {
const seen = new Set();
let j = i;
while (j < s.length && !seen.has(s[j])) {
seen.add(s[j]);
j++;
}
max = Math.max(max, j - i);
}
return max;
}
Pros:
Simple to understand
No advanced concepts needed
Cons:
O(n²) time complexity
Inefficient for long strings
** Optimized Sliding Window with Set**
function lengthOfLongestSubstring(s: string): number {
let start = 0;
let end = 0;
let maxLength = 0;
const charSet = new Set();
while (end < s.length) {
if (!charSet.has(s[end])) {
charSet.add(s[end]);
maxLength = Math.max(maxLength, end - start + 1);
end++;
} else {
charSet.delete(s[start]);
start++;
}
}
return maxLength;
}
Pros:
O(n) time complexity
Easier to visualize than the map approach
Cons:
Potentially more operations than the optimal solution
Worst case O(2n) time when many duplicates exist
Edge Cases to Consider
Empty string
All characters the same ("aaaaa")
No repeating characters ("abcdef")
Mixed case with special characters
Very long strings (performance testing)
Performance Considerations
The optimal solution:
Processes each character exactly once
Uses efficient hash map operations (O(1) average case)
Minimizes unnecessary computations
Works well with Unicode characters
Common Mistakes to Avoid
Not properly handling window adjustment: Moving start to just prevIndex instead of prevIndex + 1
Forgetting to update the character's latest position in the map
Incorrect length calculation: Using end - start instead of end - start + 1
Not considering all test cases: Especially edge cases with 0 or 1 length strings
**Real-world Applications
**This algorithm has practical uses in:
Text editors (finding unique sequences)
Data analysis (pattern detection)
Bioinformatics (DNA sequence analysis)
Cryptography (finding unique character patterns)
Conclusion
The sliding window with hash map tracking provides the best combination of:
🔥 Optimal O(n) time complexity
🔥 Efficient space usage
🔥 Clean implementation
🔥 Robust handling of all edge cases
Understanding this pattern will help with many other string manipulation and subarray problems. The sliding window technique is a powerful tool to have in your algorithm toolbox!