java solution using array


  • 0
    2
    public class Solution {
        public boolean checkInclusion(String s1, String s2) {
            int[] a1 = new int[26];
            for (int i = 0; i < s1.length(); i++) {
                a1[s1.charAt(i) - 'a']++;
            }
            int[] a2 = new int[26];
            for (int i = 0; i < s2.length(); i++) {
                if (i >= s1.length()) {
                    a2[s2.charAt(i - s1.length()) - 'a']--;
                }
                a2[s2.charAt(i) - 'a']++;
                if (i >= s1.length() - 1 && Arrays.equals(a1, a2)) {
                    return true;
                }
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.