Longest Substring Without Repeating Characters
Problem Statement:ā
Given a String, find the length of longest substring without any repeating character.
ā Solution: Brute Force ā Sliding Window (Set) ā Optimized Sliding Window (Map)ā
Solution: Brute Forceā
int lengthOfLongestSubstring(string s) {
if(s.empty()) return 0;
int maxLength = INT_MIN;
for(int i = 0; i < s.size(); i++) {
unordered_set<char> seenChars;
for(int j = i; j < s.size(); j++) {
if(seenChars.find(s[j]) != seenChars.end()) {
maxLength = max(maxLength, j - i);
break;
}
seenChars.insert(s[j]);
}
// Handle when substring reaches the end
maxLength = max(maxLength, (int)(s.size() - i));
}
return maxLength;
}
Solution: Sliding Window with Setā
int lengthOfLongestSubstring(string s) {
if(s.empty()) return 0;
int left = 0, right = 0;
int maxLength = INT_MIN;
unordered_set<char> seenChars;
while(right < s.size()) {
if(seenChars.find(s[right]) != seenChars.end()) {
while(left < right && seenChars.find(s[right]) != seenChars.end()) {
seenChars.erase(s[left]);
left++;
}
}
seenChars.insert(s[right]);
maxLength = max(maxLength, right - left + 1);
right++;
}
return maxLength;
}
Solution: Optimized Sliding Window with Mapā
int lengthOfLongestSubstring(string s) {
if(s.empty()) return 0;
map<char, int> lastSeen; // Stores character and its latest index
int left = 0;
int maxLength = INT_MIN;
for(int right = 0; right < s.size(); right++) {
if(lastSeen.find(s[right]) != lastSeen.end()) {
left = max(left, lastSeen[s[right]] + 1);
}
lastSeen[s[right]] = right;
maxLength = max(maxLength, right - left + 1);
}
return maxLength;
}
š How It Worksā
- Brute Force: Checks all possible substrings using two loops and a set to check for duplicates.
- Sliding Window with Set:
- Maintains a window using
leftandrightpointers. - Expands
rightuntil a duplicate is found. - Shrinks
leftuntil the duplicate is removed.
- Maintains a window using
- Optimized Sliding Window with Map:
- Uses
map<char, int>to record last seen index. - Directly moves
leftpointer tolastSeen[s[right]] + 1whenever a duplicate is found. - Ensures each character is processed only once efficiently.
- Uses
š§© Key Formula / Recurrenceā
- For Optimized Sliding Window:
left = max(left, lastSeen[s[right]] + 1)maxLength = max(maxLength, right - left + 1)
ā±ļø Time & Space Complexityā
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Brute Force | O(N²) | O(N) |
| Sliding Window + Set | O(N) | O(M) |
| Optimized Sliding Window | O(N) ā | O(M) ā |
Where:
- N = length of
s - M = size of character set (128 for ASCII)
ā ļø Edge Casesā
- Empty string ā returns 0
- All same characters ā longest substring length = 1
- No repeating characters ā length equals string size
š” Other Approachesā
| Approach | Time Complexity | Notes |
|---|---|---|
| Brute Force | O(N²) | Too slow for interviews |
| Sliding Window + Set | O(N) | Good but extra checking |
| Optimized Sliding Window | O(N) ā | Most efficient for interviews |
- For ASCII strings, replacing
mapwithvector<int>can improve performance further.
š Related Problemsā
- LeetCode 3: Longest Substring Without Repeating Characters
- LeetCode 76: Minimum Window Substring
- LeetCode 159: Longest Substring with At Most Two Distinct Characters
- GFG: Longest Distinct Characters Substring
š ļø Other Notes (Optional)ā
- Real-world analogy: Like walking through a street of unique shops; if you see a repeat shop, you restart counting from after your last visit.
- Interview Tip: Always explain sliding window with both expanding and shrinking the window as you iterate through the array.
š¬