Input:
s: "cbaebabacd" p: "abc"
Output:
[0, 6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
classSolution{ public List<Integer> findAnagrams(String s, String p){ List<Integer> result = new ArrayList(); if (p.length() > s.length()) { return result; }
int s_length = s.length(), p_length = p.length(); char[] s_array = s.toCharArray(); int[] src; int[] dest = newint[26];
for (int i = 0; i < p_length; i++) { dest[p.charAt(i) - 'a']++; }
for (int i = 0; i < s_length - p_length + 1; i++) { src = newint[26]; for (int j = 0; j < p_length; j++) { src[s_array[i + j] - 'a']++; } int index = 0; boolean flag = true; while (index < 26 && flag) { if (src[index] != dest[index]) { flag = false; break; } index++; } if (flag){ result.add(i); } }