Skip to content

Commit

Permalink
Go: 567-Permutation-in-String.go
Browse files Browse the repository at this point in the history
  • Loading branch information
imrushi authored Sep 13, 2022
1 parent 1a07036 commit 108f281
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions go/567-Permutation-in-String.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 108f281

Please sign in to comment.