Skip to content

Commit 0ace76c

Browse files
committed
438/kt-1
1 parent 631f156 commit 0ace76c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=438 lang=golang
3+
*
4+
* [438] Find All Anagrams in a String
5+
*/
6+
func findAnagrams(s string, p string) []int {
7+
if len(p) > len(s) {
8+
return []int{}
9+
}
10+
11+
sLen := len(s)
12+
pLen := len(p)
13+
14+
i := 0
15+
j := 0
16+
count := pLen
17+
18+
pCount := map[byte]int{}
19+
20+
for i := range p {
21+
pCount[p[i]]++
22+
}
23+
24+
ret := []int{}
25+
26+
for j < sLen {
27+
si := s[i]
28+
sj := s[j]
29+
if pCount[sj] > 0 {
30+
count--
31+
}
32+
pCount[sj]--
33+
j++
34+
if count == 0 {
35+
ret = append(ret, i)
36+
}
37+
if j - i == pLen {
38+
if pCount[si] >= 0 {
39+
count++
40+
}
41+
pCount[si]++
42+
i++
43+
}
44+
}
45+
46+
return ret
47+
}

0 commit comments

Comments
 (0)