-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindAllAnagramsinaString.java
52 lines (39 loc) · 1.35 KB
/
FindAllAnagramsinaString.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
45
46
47
48
49
50
51
52
package slidingwindows;
import java.util.*;
import utils.LeetCode;
import utils.Level;
@LeetCode(no =438 ,
level = Level.MEDIUM ,
title = "Find All Anagrams in a String",
url="https://leetcode.com/problems/find-all-anagrams-in-a-string/")
public class FindAllAnagramsinaString {
public List<Integer> findAnagrams(String s, String p) {
List<Integer> results= new ArrayList<>();
Map<Character, Integer> freq= new HashMap<>();
for (int i = 0; i < p.length(); i++) {
char current= p.charAt(i);
freq.compute(current, (key, value)-> value==null?1:value+1);
}
int start=0;
int count=0;
for (int end = 0; end < s.length(); end++) {
char current= s.charAt(end);
if (freq.containsKey(current)){
freq.put(current, freq.get(current)-1);
if(freq.get(current)==0) count++;
}
if(freq.size()==count) {
results.add(start);
}
if(end>=p.length()-1){
char charAtSt= s.charAt(start);
if(freq.containsKey(charAtSt)){
if(freq.get(charAtSt)==0) count--;
freq.put(charAtSt, freq.get(charAtSt)+1);
}
start++;
}
}
return results;
}
}