438. Find All Anagrams in a String

Sliding window: array

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        //sliding window: array
        int[] memo = new int[26];
        for(int i=0; i<s1.length(); i++){
            memo[s1.charAt(i)-'a']++;
        }
        
        int count = s1.length();
        for(int i=0; i<s2.length(); i++){
            if (memo[s2.charAt(i)-'a']-- >0) count--;
            
            if (i>s1.length()-1 && memo[s2.charAt(i-s1.length())-'a']++ >=0) count++;
            
            if (count==0) return true;
        }
        return false;
    }
}

Last updated