forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0567-permutation-in-string.go
44 lines (40 loc) · 1001 Bytes
/
0567-permutation-in-string.go
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
func checkInclusion(s1 string, s2 string) bool {
if len(s1) > len(s2){
return false
}
s1Count, s2Count := [26]int{}, [26]int{}
for i, _ := range s1 {
s1Count[s1[i] - 'a']++
s2Count[s2[i] - 'a']++
}
matches := 0
for i:=0;i<26;i++ {
if s1Count[i] == s2Count[i] {
matches += 1
} else {
matches += 0
}
}
l := 0
for r:=len(s1);r<len(s2);r++{
if matches == 26 {
return true
}
index := s2[r] - 'a'
s2Count[index]++
if s1Count[index] == s2Count[index]{
matches++
} else if s1Count[index] + 1 == s2Count[index]{
matches--
}
index = s2[l] - 'a'
s2Count[index]--
if s1Count[index] == s2Count[index]{
matches++
} else if s1Count[index] - 1 == s2Count[index]{
matches--
}
l++
}
return matches == 26
}