When to use it
Longest/shortest substring with a property, fixed-size max/min, “at most K distinct”, anagrams in a string.
Pattern
Grow and shrink a contiguous segment [L, R] while tracking a constraint (sum, distinct count, validity) so every element enters and leaves the window at most once.
Longest/shortest substring with a property, fixed-size max/min, “at most K distinct”, anagrams in a string.
Reference implementation in C#, Python, Java, JavaScript, and C++.
Gives O(n) solutions for substring/subarray problems that look like they need O(n²).
Drill Sliding Window with Strings problems — filter the bank and solve 2–3 Easy/Medium items while saying the invariant out loud.
Related Strings problems — open any one and apply Sliding Window.
Longest Substring Without Repeating Characters
Sliding Window
Longest Palindromic Substring
DP / Expand
Zigzag Conversion
Simulation
String to Integer (atoi)
Parsing
Regular Expression Matching
DP
Integer to Roman
Greedy
Roman to Integer
Greedy
Longest Common Prefix
String
Letter Combinations of a Phone Number
Backtracking