Overview
- Sliding Window Technique is a popular method in algorithmic problems because of its high efficiency (mostly linear)
- It helps to avoid computing repetitive problems by computing only the new part that's introduced in the data set and discarding the one that moves out, usually 1 at a time.
- There are 2 categories of SWT problems:
- Fixed sized window: The window remains of a consant size (say k), which slides/moves along a data structure. Imagine sliding window panel along a long ridge
- Variable sized window: The window fluctuates in size, but still slides/moves along a data structure. Imagine a spring compressing and expanding while moving along a plane
- There are plenty of articles explaining the what and how, so this post isn't gonna repeat on that. This post is to sketch out a rough algorithm which can be tweaked for most problems on SW
- This is meant to be a continuosly improving post and I'll keep updating it and add more problems as I progress.
- Collaboration works wonders so I'd encourage anyone reading this post to feel free to contribute for the benefit of the community.
Sample Problem: Longest Substring Without Repeating Characters.
Given a string s, find the length of the longest substring without repeating characters.
Algorithm
- Use a hashMap to keep track of the latest index of each letter
- Keeping the left pointer at rest, move the right pointer by 1 letter at a time.
- When a repeating character is encountered, update the maxLength and move the left pointer to max{left pointer, old last occurence of this character as available in the map}. We do a max because we don't want to take the left pointer backwards at any time (e.g. in "abba"), it will only move forward or stay still.
- return max {right-left, maxLength}. Doing this outside the loop is essential as it handles strings with all unique chars.
T/S: O(n)/O(a), where n = s.length, a = size of character set
public int lengthOfLongestSubstring(String s) {
var n = s.length();
if (n < 2)
return n;
var left = 0;
var right = 0;
var maxLength = 0;
var map = new HashMap<Character, Integer>(); // map of char to its latest index
while (right < n) {
var ch = s.charAt(right);
if (map.containsKey(ch)) {
maxLength = Math.max(maxLength, right - left); // record max window size
left = Math.max(left, map.get(ch) + 1); // shrink window from left
}
map.put(ch, right);
right++ // expand window to the right
}
return Math.max(maxLength, right - left);
}Question Bank
A slight variation using the template for Sliding Window that can be used in the following problems that follow a similar pattern:
Fix Sized Window
- 30. Substring with Concatenation of All Words
- 187. Repeated DNA Sequences
- 438. Find All Anagrams in a String
- 567. Permutation in String
- 643. Maximum Average Subarray I
- 995. Minimum Number of K Consecutive Bit Flips
- 1100. Find K-Length Substrings With No Repeated Characters (P)
- 1151. Minimum Swaps to Group All 1's Together (P)
- 1176. Diet Plan Performance (P)
- 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
- 1423. Maximum Points You Can Obtain from Cards
- 1456. Maximum Number of Vowels in a Substring of Given Length
- 1652. Defuse the Bomb
- 1876. Substrings of Size Three with Distinct Characters
- 2090. K Radius Subarray Averages
- 2134. Minimum Swaps to Group All 1's Together II
- 2269. Find the K-Beauty of a Number
- 2379. Minimum Recolors to Get K Consecutive Black Blocks
- 2461. Maximum Sum of Distinct Subarrays With Length K
- 3191. Minimum Operations to Make Binary Array Elements Equal to One I
- 3206. Alternating Groups I
- 3208. Alternating Groups II
- 3254. Find the Power of K-Size Subarrays I
- 3439. Reschedule Meetings for Maximum Free Time I
Variable Sized Window
- 3. Longest Substring Without Repeating Characters
- 76. Minimum Window Substring
- 159. Longest Substring with At Most Two Distinct Characters
- 209. Minimum Size Subarray Sum
- 340. Longest Substring with At Most K Distinct Characters (P)
- 424. Longest Repeating Character Replacement
- 487. Max Consecutive Ones II (P)
- 795. Number of Subarrays with Bounded Maximum
- 862. Shortest Subarray with Sum at Least K
- 904. Fruit Into Baskets
- 992. Subarrays with K Different Integers
- 1004. Max Consecutive Ones III
- 1208. Get Equal Substrings Within Budget
- 1297. Maximum Number of Occurrences of a Substring
- 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
- 1358. Number of Substrings Containing All Three Characters
- 1574. Shortest Subarray to be Removed to Make Array Sorted
- 1493. Longest Subarray of 1's After Deleting One Element
- 1695. Maximum Erasure Value
- 1838. Frequency of the Most Frequent Element
- 1852. Distinct Numbers in Each Subarray (P)
- 2024. Maximize the Confusion of an Exam
- 2062. Count Vowel Substrings of a String
- 2302. Count Subarrays With Score Less Than K
- 2444. Count Subarrays With Fixed Bounds
- 2516. Take K of Each Character From Left and Right
- 2537. Count the Number of Good Subarrays
- 2799. Count Complete Subarrays in an Array
- 2962. Count Subarrays Where Max Element Appears at Least K Times
- 3095. Shortest Subarray With OR at Least K I
- 3097. Shortest Subarray With OR at Least K II
- 3305. Count of Substrings Containing Every Vowel and K Consonants I
- 3306. Count of Substrings Containing Every Vowel and K Consonants II
Legend: P = Premium
Comments and contributions for improvement are appreciated. Thanks!
Comments (15)

