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.