-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindAnagrams.java
More file actions
59 lines (44 loc) · 1.64 KB
/
Copy pathFindAnagrams.java
File metadata and controls
59 lines (44 loc) · 1.64 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.List;
import java.util.ArrayList;
class FindAnagrams {
public static void findAnagrams(String s, String p) {
if (s == null || s.isEmpty()) {
return new ArrayList<Integer>();
}
List<Integer> output = new ArrayList<>();
int[] characterHash = new int[256];
for (char ch : p.toCharArray()) {
characterHash[ch]++;
}
int left = 0 , right = 0, count = p.length();
while (right < s.length()) {
//move right everytime, if the character exists in p's hash, decrease the count
//currrent hash value >= 1 means that characters is existing in p
if (characterHash[s.charAt(right)] >= 1) {
count--;
}
characterHash[s.charAt(right)]--;
right++;
//when the count is 0, we have find the right anagram
if (count == 0) {
output.add(left);
}
// if window's size is equal to p, then we move to left to find new match
// ++ to reset the hash because we kicked out the left
// only increase the count if the character is in p
// the count >= 0 indicate it was original in the hash, cos it wont go below 0
//
if (right - left == p.length() ) {
if (characterHash[s.charAt(left)] >= 0) {
count++;
}
characterHash[s.charAt(left)]++;
left++;
}
}
return output;
}
public static void main(String[] args) {
findAnagrams("cbaebabacd", "abc");
}
}