Back to blog
Mar 17, 2024
3 min read

Algorithms: Sliding Window

How can sliding window algorithm solve some problems

Sliding window technique

Sliding windows can be useful in searching through arrays. A window is a selected sub array of a main array. While searching for something, a specific word or duplicate characters in a string you can start by picking a window and then sliding that window using two pointers to demark the two boundaries. Move the right pointer to slide the window to the right while using left pointer to narrow down the window.

Try the contains duplicates problems on leetcode to practice the sliding windows.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

The problem input is a linear data structure, linked list, array or string. Task is usually to find substrings, subarrays with some condition.

From LinkedIn

πŸš€ Day 32/90: Solved 3 DSA problems in Java and learnt about sliding window Pattern πŸ“šπŸ’» I solved an easy #geekforgeeks problem and an easy and medium #leetcode problem. The problems and code are in the image below, as well as in my leetcode profile (https://lnkd.in/gcnZ89PX) and GFG profile(https://lnkd.in/esBsNhGb).

πŸ”Ί Max Sum Subarray of size K: Time complexity - O(N), Space complexity - O(1) πŸ”Ί 121. Best Time to Buy and Sell Stock: Time complexity - O(N), Space complexity - O(1) πŸ”Ί 424. Longest Repeating Character Replacement: Time complexity - O(N), Space complexity - O(1) (two implementations with an optimized code)

πŸŒπŸ’‘ Unleashing the Power of Sliding Window Algorithms: A Dynamic Approach to Data Processing πŸŒπŸ’‘ Understanding the versatility of sliding windows is a game-changer in problem-solving. πŸŒŠπŸ”πŸ’» 🧐 What are Sliding Window Algorithms? Sliding window algorithms process data streams by segmenting them into fixed-size chunks. The window then glides over the data, one element at a time, performing operations on each chunk. It’s a technique that brings efficiency and versatility to the table.

🎯 Identifying Sliding Window Problems: πŸ•΅οΈβ€β™‚οΈ Substrings/subarrays: If a problem involves finding a particular substring or subarrays in a larger string or array, sliding windows might be the key. πŸ“Š Calculating Statistics: Problems requiring the calculation of statistics over a subset of data points, with updates as the window slides, often align with sliding window solutions. ⬆️⬇️ Finding Extremes: Whether it’s identifying the maximum or minimum value within a window of data points, sliding windows excel in such scenarios. πŸ”„ Types of Sliding Window Algorithms: πŸšͺ Fixed-size Window: The simplest type, where the window size remains constant. πŸ“ Variable-size Window: Adaptable windows that change size based on data characteristics. πŸ“ˆ Count-based Window: Tracks the frequency of a specific element within the window. βž• Sum-based Window: Keeps tabs on the sum of elements within the window.

🌐 Real-world Applications: πŸ“‰ Max Subarray Sum: Finding the maximum sum of a subarray of a given size. πŸš€ Unique Substring Length: Discovering the longest substring without repeating characters. πŸ” Substring Occurrences: Counting the number of occurrences of a substring in a string.

🎬 Conclusion: Sliding window algorithms are a potent and versatile tool, simplifying the solution to a myriad of problems. They are easy to implement and exhibit remarkable efficiency.