Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money

The title

Java code

class Solution {
    public boolean checkInclusion(String s1, String s2) {
        if(s1.length()>s2.length()){
            return false;
        }
        
        int[] arr1 = new int[26];
        int[] arr2 = new int[26];
        
        int len1 = s1.length();
        int len2 = s2.length();
        
        for(int i =0; i<len1; i++){ arr1[s1.charAt(i)-'a'] + +; arr2[s2.charAt(i)-'a'] + +; }int cha=len2-len1;
        
        for(int i =0; i<cha; i++){if(Arrays.equals(arr1,arr2)){
                return true;
            }
            arr2[s2.charAt(i+len1)-'a'] + +; arr2[s2.charAt(i)-'a']--;
        }
        returnArrays.equals(arr1,arr2); }}Copy the code

This problem I also wrote quite long, their train of thought is always very limited open. Read other people’s problem solution, after understanding, oneself wrote again. I feel fulfilled. The sensory algorithms still need to be practiced to be memorable.

So the general idea of using the sliding window method is, first of all, since the strings are all lowercase letters a-Z, first of all, create two corresponding arrays for the two strings, The difference of AScall code -a can be used to store the occurrence and number of letters in each string by comparing the values of ARr1 and ARR2 of the distance from the leftmost end of S2 to s1.length, and then storing the letters corresponding to the remaining values of S2 into ARR2. At the same time, different values in arr2 and S1 are removed.