Mid
Detailed answer
Sliding Window
DSA & Coding Interviews
How do you find the longest substring without repeating characters?
Short answer: Sliding window with a map/set of characters in the current window. Expand right; when a duplicate appears, shrink left until the window is unique again. Track max window length.
Sample solution
C#
int LengthOfLongestSubstring(string s) {
var last = new Dictionary<char, int>();
int left = 0, best = 0;
for (int right = 0; right < s.Length; right++) {
char c = s[right];
if (last.TryGetValue(c, out int prev) && prev >= left)
left = prev + 1;
last[c] = right;
best = Math.Max(best, right - left + 1);
}
return best;
}
Complexity
Time O(n), Space O(min(n, alphabet)).
Edge cases to mention
- Empty string
- All unique
- All same character
- Unicode / case sensitivity
Name the pattern “variable-size sliding window” — it signals seniority.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png