Sliding Window Technique: Concepts, Framework, and LeetCode Examples
This article introduces the sliding window algorithmic technique, explains its relation to TCP flow control, demonstrates its implementation with Java code for maximum subarray sum, longest substring without repeats, and minimum window substring problems, and provides a reusable framework for solving similar LeetCode challenges.
When practicing LeetCode, many encounter classic sliding‑window problems that require maintaining a moving window over an array or string to update answers efficiently.
What is a sliding window? In networking, the TCP sliding‑window protocol controls flow; similarly, algorithmic sliding windows maintain a window (array, queue, or hash set) that moves forward while updating results.
Example 1 – Maximum sum of a length‑k subarray
输入:arr [] = {100,200,300,400} k = 2
输出:700
解释:300 + 400 = 700Brute‑force uses two nested loops with O(k·n) time:
public int maxSum(int[] arr, int k) {
int size = arr.length;
int maxSum = 0;
for (int i = 0; i < size - k + 1; i++) {
int currentSum = 0;
for (int j = 0; j < k; j++) {
currentSum = currentSum + arr[i + j];
}
maxSum = Math.max(currentSum, maxSum);
}
return maxSum;
}Using a sliding window reduces the complexity to O(n) :
public int maxSum1(int[] arr, int k) {
int size = arr.length;
if (size < k) return -1;
int maxSum = 0;
for (int i = 0; i < k; i++) {
maxSum += arr[i];
}
int sum = maxSum;
for (int i = k; i < size; i++) {
sum = sum + arr[i] - arr[i - k];
maxSum = Math.max(maxSum, sum);
}
return maxSum;
}Problems solvable by sliding windows include longest substring without repeating characters, minimum covering substring, substring concatenation of all words, longest substring with at most two distinct characters, minimum size subarray sum, sliding‑window maximum, string permutations, minimum window subsequence, etc.
General sliding‑window framework (pseudocode)
int left = 0, right = 0;
while (right < s.size()) {
// expand window
window.add(s[right]);
right++;
while (window needs shrink) {
// shrink window
window.remove(s[left]);
left++;
}
}Example 2 – Longest substring without repeating characters
int lengthOfLongestSubstring(String s) {
int len = s.length();
Set
window = new HashSet<>();
int left = 0, right = 0, res = 0;
while (right < len) {
char c = s.charAt(right);
right++;
while (window.contains(c)) {
window.remove(s.charAt(left));
left++;
}
window.add(c);
res = Math.max(res, window.size());
}
return res;
}Example 3 – Minimum window substring (cover all characters of T)
class Solution {
public String minWindow(String s, String t) {
if (s.length() == 0 || s.length() < t.length()) return "";
int sLen = s.length();
Map
lookup = new HashMap<>();
for (int i = 0; i < sLen; i++) lookup.put(s.charAt(i), 0);
for (int i = 0; i < t.length(); i++) {
char c = t.charAt(i);
if (lookup.containsKey(c)) lookup.put(c, lookup.get(c) + 1);
else return "";
}
int left = 0, right = 0, minLen = Integer.MAX_VALUE, tCount = t.length();
String result = "";
while (right < sLen) {
char c = s.charAt(right);
if (lookup.get(c) > 0) tCount--;
lookup.put(c, lookup.get(c) - 1);
right++;
while (tCount == 0) {
if (minLen > right - left) {
minLen = right - left;
result = s.substring(left, right);
}
char c2 = s.charAt(left);
if (lookup.get(c2) == 0) tCount++;
lookup.put(c2, lookup.get(c2) + 1);
left++;
}
}
return result;
}
}The sliding‑window technique thus provides a systematic way to transform nested‑loop solutions into linear‑time algorithms for a wide range of string and array problems.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.