Fixed Sliding Window

For fixed Sliding window problems, when we encounter a valid window, we need to check for the constraints, and if the constraints are satisfied, we can add the required data to result set.

Template

public void method(String s) {
        // Create a result set/variable
  
        // Check for edge cases
        if(s.length() == 0 || s == null || s.isBlank()) {
            // Handle it appropriately or return the result
        }
    
        // Create a ds, which is required to hold the data of the window
        holder = Map, Set, int[]

        int start = 0;
        // Iterate it over input
        for(int end=0;end<s.length();end++) {
            // Get the current element
            char ch = s.charAt(end);

            // Store the current element desired property in holder
            holder.put(ch, freq);

            // Check if the given window is valid
            // If valid, add the required property (like index, frequency, length) to result set

            // Reduce the size of window by moving forward start pointer
        }
        return result;
    }

Example Problem:

https://leetcode.com/problems/find-all-anagrams-in-a-stringarrow-up-right

You can see the above template being followed here

Last updated