forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_438.java
43 lines (38 loc) · 1.13 KB
/
_438.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _438 {
public static class Solution1 {
/**
* Sliding Window
*/
public List<Integer> findAnagrams(String s, String p) {
List<Integer> result = new ArrayList();
int[] hash = new int[26];
for (char c : p.toCharArray()) {
hash[c - 'a']++;
}
int start = 0;
int end = 0;
int count = p.length();
while (end < s.length()) {
if (hash[s.charAt(end) - 'a'] > 0) {
count--;
}
hash[s.charAt(end) - 'a']--;
end++;
if (count == 0) {
result.add(start);
}
if ((end - start) == p.length()) {
if (hash[s.charAt(start) - 'a'] >= 0) {
count++;
}
hash[s.charAt(start) - 'a']++;
start++;
}
}
return result;
}
}
}