1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public List<Integer> findAnagrams(String s, String p) { List<Integer> ans = new ArrayList<>(); int n = s.length(), m = p.length(); int[] c1 = new int[26], c2 = new int[26]; for (int i = 0; i < m; ++i) ++c2[p.charAt(i) - 'a']; for (int l = 0, r = 0; r < n; ++r) { ++c1[s.charAt(r) - 'a']; if (r - l + 1 > m) --c1[s.charAt(l++) - 'a']; if (check(c1, c2)) ans.add(l); } return ans; }
private boolean check(int[] c1, int[] c2) { for (int i = 0; i < 26; ++i) { if (c1[i] != c2[i]) return false; } return true; } }
|