package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/permutation-in-string/
 */
public class Problem567 {
    public boolean checkInclusion(String s1, String s2) {
        Map<Character, Integer> map1 = charCount(s1);
        for (int i = 0; i + s1.length() <= s2.length(); i++) {
            String sub = s2.substring(i, i + s1.length());
            Map<Character, Integer> map2 = charCount(sub);
            if (map1.equals(map2)) {
                return true;
            }
        }
        return false;
    }

    private static Map<Character, Integer> charCount(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (!map.containsKey(s.charAt(i))) {
                map.put(s.charAt(i), 1);
            } else {
                map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
            }
        }
        return map;
    }

    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
    }
}

@@ -33,37 +33,14 @@
 til.
-HashMap;%0Aimport java.util.Map
+Arrays
 ;%0A%0A/
@@ -198,35 +198,20 @@
     
-Map%3CCharacter, Integer%3E map
+int%5B%5D counts
 1 = 
@@ -363,35 +363,20 @@
     
-Map%3CCharacter, Integer%3E map
+int%5B%5D counts
 2 = 
@@ -411,20 +411,22 @@
 if (
-map1
+Arrays
 .equals(
 map2
@@ -421,19 +421,31 @@
 .equals(
-map
+counts1, counts
 2)) %7B%0A  
@@ -543,39 +543,21 @@
  static 
-Map%3CCharacter, Integer%3E
+int%5B%5D
  charCou
@@ -583,53 +583,34 @@
     
-Map%3CCharacter, Integer%3E map = new HashMap%3C%3E()
+int%5B%5D counts = new int%5B26%5D
 ;%0A  
@@ -670,29 +670,15 @@
     
-if (!map.
 co
+u
 nt
-ainsKey(
+s%5B
 s.ch
@@ -688,152 +688,18 @@
 t(i)
-)) %7B%0A                map.put(s.charAt(i), 1);%0A            %7D else %7B%0A                map.put(s.charAt(i), map.get(s.charAt(i)) + 1);%0A            %7D
+ - 'a'%5D++;
 %0A   
@@ -724,11 +724,14 @@
 urn 
-map
+counts
 ;%0A  
@@ -824,18 +824,16 @@
 m567();%0A
-//
         
@@ -899,18 +899,16 @@
 // true%0A
-//
         
@@ -975,18 +975,16 @@
 / false%0A
-//
         

Solve problem 567