package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/find-all-anagrams-in-a-string/
 */
public class Problem438 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();
        char[] sortedP = p.toCharArray();
        Arrays.sort(sortedP);
        for (int i = 0; i < s.length(); i++) {
            if (i + p.length() > s.length()) {
                break;
            }
            char[] sortedSub = s.substring(i, i + p.length()).toCharArray();
            Arrays.sort(sortedSub);
            if (Arrays.equals(sortedP, sortedSub)) {
                result.add(i);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem438 prob = new Problem438();
        System.out.println(prob.findAnagrams("cbaebabacd", "abc")); // [0, 6]
        System.out.println(prob.findAnagrams("abab", "ab")); // [0, 1, 2]
    }
}

@@ -309,40 +309,122 @@
     
-char%5B%5D sortedP
+int%5B%5D pArray = new int%5B26%5D;%0A        for (int i = 0; i %3C p.length(); i++) %7B%0A            int idx
  = p.
-toC
+c
 harA
-rray()
+t(i) - 'a'
 ;%0A  
@@ -433,29 +433,36 @@
     
+ 
+   p
 Array
-s.sort(sortedP);
+%5Bidx%5D++;%0A        %7D
 %0A   
@@ -605,71 +605,134 @@
     
-char%5B%5D sortedSub = s.substring(i, i + p.length()).toCharArray()
+int%5B%5D sArray = new int%5B26%5D;%0A            for (int j = i; j %3C i + p.length(); j++) %7B%0A                int idx = s.charAt(j) - 'a'
 ;%0A  
@@ -745,80 +745,85 @@
     
+ 
+   s
 Array
-s.sort(sortedSub);%0A            if (Arrays.equals(sortedP, sortedSub
+%5Bidx%5D++;%0A            %7D%0A            if (Arrays.equals(pArray, sArray
 )) %7B
@@ -910,255 +910,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem438 prob = new Problem438();%0A        System.out.println(prob.findAnagrams(%22cbaebabacd%22, %22abc%22)); // %5B0, 6%5D%0A        System.out.println(prob.findAnagrams(%22abab%22, %22ab%22)); // %5B0, 1, 2%5D%0A    %7D
 %0A%7D%0A

Solve problem 438