1. Sliding Window with Character Set (Shrinking)
Maintain active window characters in a Set. Whenever s[right] already exists in the set, evict s[left] one character at a time while advancing left.
Each character is inserted once by right and evicted at most once by left (<= 2N operations).
Set stores at most the active window's unique characters (bounded by alphabet size m).
How It Works
Shrinking the Left Boundary Step-by-Step
Before optimizing with last-seen indices, model window uniqueness with a plain Hash Set:
- Expand
rightfrom0ton - 1. - While
s[right]is already insideseen:
- Remove
s[left]fromseen. - Advance
left++.
- Insert
s[right]intoseenand updatemaxLen = max(maxLen, right - left + 1).
Even though an inner while loop appears, left visits each index at most once across the entire run, keeping total time O(n) (up to 2n operations vs n with the jump map).
Step-by-Step Dry Run
Right adds indices 0..2 into seen set. maxLen reaches 3.
seen = {a, b, c}max = 3- +Simple invariant: set contents literally mirror the active window
- +Requires no index arithmetic beyond left++
- -Up to 2N passes over characters compared to single-jump N passes with an index map
Reference Implementation
Complete implementation for Sliding Window with Character Set (Shrinking) in Python, JavaScript, Java, C++, and C.
def length_of_longest_substring_set(s: str) -> int:seen = set()left = 0max_len = 0for right, ch in enumerate(s):while ch in seen:seen.remove(s[left])left += 1seen.add(ch)max_len = max(max_len, right - left + 1)return max_len
function lengthOfLongestSubstringSet(s) {
const seen = new Set();
let left = 0, maxLen = 0;
for (let right = 0; right < s.length; right++) {
while (seen.has(s[right])) {
seen.delete(s[left++]);
}
seen.add(s[right]);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}import java.util.HashSet;
import java.util.Set;
public class Solution {
public static int lengthOfLongestSubstringSet(String s) {
Set<Character> seen = new HashSet<>();
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
char c = s.charAt(right);
while (seen.contains(c)) {
seen.remove(s.charAt(left++));
}
seen.add(c);
maxLen = Math.max(maxLen, right - left + 1);
}
return maxLen;
}
}#include <string>
#include <vector>
#include <algorithm>
int lengthOfLongestSubstringSet(const std::string& s) {
std::vector<bool> inWin(256, false);
int left = 0, maxLen = 0;
for (int right = 0; right < static_cast<int>(s.size()); right++) {
unsigned char c = static_cast<unsigned char>(s[right]);
while (inWin[c]) {
inWin[static_cast<unsigned char>(s[left++])] = false;
}
inWin[c] = true;
maxLen = std::max(maxLen, right - left + 1);
}
return maxLen;
}#include <stdbool.h>
#include <string.h>
int lengthOfLongestSubstringSet(const char* s) {
bool inWin[256] = {false};
int left = 0, maxLen = 0, n = (int)strlen(s);
for (int right = 0; right < n; right++) {
unsigned char c = (unsigned char)s[right];
while (inWin[c]) {
inWin[(unsigned char)s[left++]] = false;
}
inWin[c] = true;
int cur = right - left + 1;
if (cur > maxLen) maxLen = cur;
}
return maxLen;
}