We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 631f156 commit 0ace76cCopy full SHA for 0ace76c
438.find-all-anagrams-in-a-string.1.go
@@ -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