Skip to content

Commit 0a7d342

Browse files
committed
FindAllAnagramsInAString438
1 parent 0ad500f commit 0a7d342

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 356
47+
# Total: 357
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 93 | 198 | 61 | 4 |
51+
| 94 | 198 | 61 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -317,6 +317,7 @@
317317
| [423. Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/ReconstructOriginalDigitsFromEnglish423.java) | Medium |
318318
| [425. Word Squares](https://leetcode.com/problems/word-squares/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/WordSquares425.java) | Hard |
319319
| [433. Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MinimumGeneticMutation433.java) | Medium |
320+
| [438. Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindAllAnagramsInAString438.java) | Easy |
320321
| [443. String Compression](https://leetcode.com/problems/string-compression/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/StringCompression443.java) | Easy |
321322
| [445. Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/AddTwoNumbersII445.java) | Medium |
322323
| [448. Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/FindAllNumbersDisappearedInAnArray448.java) | Easy |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Given a string s and a non-empty string p, find all the start indices of
3+
* p's anagrams in s.
4+
*
5+
* Strings consists of lowercase English letters only and the length of both
6+
* strings s and p will not be larger than 20,100.
7+
*
8+
* The order of output does not matter.
9+
*
10+
* Example 1:
11+
* Input:
12+
* s: "cbaebabacd" p: "abc"
13+
* Output:
14+
* [0, 6]
15+
* Explanation:
16+
* The substring with start index = 0 is "cba", which is an anagram of "abc".
17+
* The substring with start index = 6 is "bac", which is an anagram of "abc".
18+
*
19+
* Example 2:
20+
* Input:
21+
* s: "abab" p: "ab"
22+
* Output:
23+
* [0, 1, 2]
24+
* Explanation:
25+
* The substring with start index = 0 is "ab", which is an anagram of "ab".
26+
* The substring with start index = 1 is "ba", which is an anagram of "ab".
27+
* The substring with start index = 2 is "ab", which is an anagram of "ab".
28+
*/
29+
30+
public class FindAllAnagramsInAString438 {
31+
public List<Integer> findAnagrams(String s, String p) {
32+
int[] map = new int[26];
33+
for (char c: p.toCharArray()) {
34+
map[c - 'a']++;
35+
}
36+
37+
List<Integer> res = new ArrayList<>();
38+
int lenS = s.length();
39+
int count = p.length();
40+
if (lenS < count) return res;
41+
char[] chars = s.toCharArray();
42+
int i = 0;
43+
int j = 0;
44+
while (j < lenS) {
45+
char c = chars[j];
46+
while (i <= j && map[c - 'a'] <= 0) {
47+
map[chars[i] - 'a']++;
48+
i++;
49+
count++;
50+
}
51+
if (i >= lenS) break;
52+
map[c - 'a']--;
53+
j++;
54+
count--;
55+
if (count == 0) {
56+
res.add(i);
57+
map[chars[i] - 'a']++;
58+
count++;
59+
i++;
60+
}
61+
}
62+
63+
return res;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)