-
Notifications
You must be signed in to change notification settings - Fork 5
/
438. Find All Anagrams in a String.java
44 lines (44 loc) · 1.69 KB
/
438. Find All Anagrams in a String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// https://leetcode.com/problems/find-all-anagrams-in-a-string/
class Solution {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> answer = new ArrayList<Integer>();
if (s.length() < p.length() ) return answer;
Map<Character, Integer> pmap = new HashMap<>();
for (Character c : p.toCharArray())
pmap.put(c, pmap.getOrDefault(c, 0)+1);
int start = 0;
int end = 0;
Map<Character, Integer> smap = new HashMap<>();
while (end < s.length()) {
char c = s.charAt(end);
smap.put(c, smap.getOrDefault(c, 0) + 1);
int windowSize = end - start + 1;
if (windowSize == p.length()) {
// check if two maps are equal
if ( pmap.equals(smap)) {
answer.add(start);
}
// decrease the count for s[start]
int ppcount = smap.get(s.charAt(start));
// if the ppcount - 1 == 0 remove it out
if (ppcount - 1 == 0) {
smap.remove(s.charAt(start));
} else {
smap.put( s.charAt(start), ppcount - 1);
}
start++;
}
end++;
}
return answer;
}
}