|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.List;
|
5 | 5 |
|
| 6 | +/**438. Find All Anagrams in a String |
| 7 | + * Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. |
| 8 | + Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. |
| 9 | +
|
| 10 | + The order of output does not matter. |
| 11 | +
|
| 12 | + Example 1: |
| 13 | + Input: |
| 14 | + s: "cbaebabacd" p: "abc" |
| 15 | + Output: |
| 16 | + [0, 6] |
| 17 | +
|
| 18 | + Explanation: |
| 19 | + The substring with start index = 0 is "cba", which is an anagram of "abc". |
| 20 | + The substring with start index = 6 is "bac", which is an anagram of "abc". |
| 21 | +
|
| 22 | + Example 2: |
| 23 | + Input: |
| 24 | + s: "abab" p: "ab" |
| 25 | + Output: |
| 26 | + [0, 1, 2] |
| 27 | +
|
| 28 | + Explanation: |
| 29 | + The substring with start index = 0 is "ab", which is an anagram of "ab". |
| 30 | + The substring with start index = 1 is "ba", which is an anagram of "ab". |
| 31 | + The substring with start index = 2 is "ab", which is an anagram of "ab".*/ |
| 32 | + |
6 | 33 | public class _438 {
|
7 |
| - /**O(m*n) solution, my original and most intuitive one, but kind of brute force.*/ |
| 34 | + /** |
| 35 | + * O(m*n) solution, my original and most intuitive one, but kind of brute force. |
| 36 | + */ |
8 | 37 | public List<Integer> findAnagrams(String s, String p) {
|
9 | 38 | List<Integer> result = new ArrayList();
|
10 |
| - for (int i = 0; i <= s.length()-p.length(); i++){ |
11 |
| - if (isAnagram(s.substring(i, i+p.length()), p)) result.add(i); |
| 39 | + for (int i = 0; i <= s.length() - p.length(); i++) { |
| 40 | + if (isAnagram(s.substring(i, i + p.length()), p)) result.add(i); |
12 | 41 | }
|
13 | 42 | return result;
|
14 | 43 | }
|
15 |
| - |
16 |
| - private boolean isAnagram(String s, String p){ |
| 44 | + |
| 45 | + private boolean isAnagram(String s, String p) { |
17 | 46 | int[] c = new int[26];
|
18 |
| - for (int i = 0; i < s.length(); i++){ |
| 47 | + for (int i = 0; i < s.length(); i++) { |
19 | 48 | c[s.charAt(i) - 'a']++;
|
20 | 49 | c[p.charAt(i) - 'a']--;
|
21 | 50 | }
|
22 |
| - |
23 |
| - for (int i : c){ |
24 |
| - if(i != 0) return false; |
| 51 | + |
| 52 | + for (int i : c) { |
| 53 | + if (i != 0) return false; |
25 | 54 | }
|
26 | 55 | return true;
|
27 | 56 | }
|
28 | 57 |
|
29 |
| - |
| 58 | + |
30 | 59 | static class SlidingWindowSolution {
|
31 |
| - /**O(n) solution inspired by this post: https://discuss.leetcode.com/topic/64434/shortest-concise-java-o-n-sliding-window-solution*/ |
32 | 60 | public List<Integer> findAnagrams(String s, String p) {
|
33 | 61 | List<Integer> result = new ArrayList();
|
34 | 62 | int[] hash = new int[26];
|
35 |
| - for (char c : p.toCharArray()){ |
| 63 | + for (char c : p.toCharArray()) { |
36 | 64 | hash[c - 'a']++;
|
37 | 65 | }
|
38 | 66 | int start = 0, end = 0, count = p.length();
|
39 |
| - while (end < s.length()){ |
40 |
| - if(hash[s.charAt(end) - 'a'] > 0){ |
| 67 | + while (end < s.length()) { |
| 68 | + if (hash[s.charAt(end) - 'a'] > 0) { |
41 | 69 | count--;
|
42 | 70 | }
|
43 | 71 | hash[s.charAt(end) - 'a']--;
|
44 | 72 | end++;
|
45 |
| - |
46 |
| - if(count == 0) result.add(start); |
47 |
| - |
48 |
| - if((end - start) == p.length()){ |
49 |
| - if(hash[s.charAt(start) - 'a'] >= 0) count++; |
| 73 | + |
| 74 | + if (count == 0) result.add(start); |
| 75 | + |
| 76 | + if ((end - start) == p.length()) { |
| 77 | + if (hash[s.charAt(start) - 'a'] >= 0) count++; |
50 | 78 | hash[s.charAt(start) - 'a']++;
|
51 | 79 | start++;
|
52 | 80 | }
|
53 | 81 | }
|
54 | 82 | return result;
|
55 | 83 | }
|
56 | 84 | }
|
57 |
| - |
58 |
| - public static void main(String...args){ |
| 85 | + |
| 86 | + public static void main(String... args) { |
59 | 87 | SlidingWindowSolution test = new SlidingWindowSolution();
|
60 | 88 | String s = "cbaebabacd";
|
61 | 89 | String p = "abc";
|
|
0 commit comments