package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/permutation-in-string/
 */
public class Problem567 {
    public boolean checkInclusion(String s1, String s2) {
        int[] counts1 = charCount(s1);
        for (int i = 0; i + s1.length() <= s2.length(); i++) {
            String sub = s2.substring(i, i + s1.length());
            int[] counts2 = charCount(sub);
            if (Arrays.equals(counts1, counts2)) {
                return true;
            }
        }
        return false;
    }

    private static int[] charCount(String s) {
        int[] counts = new int[26];
        for (int i = 0; i < s.length(); i++) {
            counts[s.charAt(i) - 'a']++;
        }
        return counts;
    }

    public static void main(String[] args) {
        Problem567 prob = new Problem567();
        System.out.println(prob.checkInclusion("ab", "eidbaooo")); // true
        System.out.println(prob.checkInclusion("ab", "eidboaoo")); // false
        System.out.println(prob.checkInclusion("dinitrophenylhydrazine", "acetylphenylhydrazine")); // false
        System.out.println(prob.checkInclusion("adc", "dcda")); // true
    }
}

@@ -237,28 +237,12 @@
     
-for (int i = 0; i + 
+if (
 s1.l
@@ -253,10 +253,9 @@
 h() 
-%3C=
+%3E
  s2.
@@ -254,37 +254,32 @@
 () %3E s2.length()
-; i++
 ) %7B%0A            
@@ -282,40 +282,199 @@
     
-String sub = s2.substring(i, i +
+return false;%0A        %7D%0A        int%5B%5D counts2 = charCount(s2.substring(0, s1.length()));%0A        if (Arrays.equals(counts1, counts2)) %7B%0A            return true;%0A        %7D%0A        for (int i =
  s1.
@@ -485,53 +485,190 @@
 th()
-);%0A            int%5B%5D counts2 = charCount(sub)
+; i %3C s2.length(); i++) %7B%0A            // some optimization to not construct counts2%0A            counts2%5Bs2.charAt(i - s1.length()) - 'a'%5D--;%0A            counts2%5Bs2.charAt(i) - 'a'%5D++
 ;%0A  
@@ -1011,435 +1011,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem567 prob = new Problem567();%0A        System.out.println(prob.checkInclusion(%22ab%22, %22eidbaooo%22)); // true%0A        System.out.println(prob.checkInclusion(%22ab%22, %22eidboaoo%22)); // false%0A        System.out.println(prob.checkInclusion(%22dinitrophenylhydrazine%22, %22acetylphenylhydrazine%22)); // false%0A        System.out.println(prob.checkInclusion(%22adc%22, %22dcda%22)); // true%0A    %7D
 %0A%7D%0A

Solve problem 567