go: add «1456. Maximum Number of Vowels in a Substring of Given Length»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4fc9cb0a11
commit
e9e304b2aa
1 changed files with 33 additions and 0 deletions
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
func maxVowels(s string, k int) int {
|
||||
isVowel := func(c byte) bool {
|
||||
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
|
||||
}
|
||||
countFirst := func() int {
|
||||
count := 0
|
||||
for i := 0; i < k; i++ {
|
||||
if isVowel(s[i]) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
count := countFirst()
|
||||
|
||||
maxCount := count
|
||||
for i := k; i < len(s); i++ {
|
||||
if isVowel(s[i-k]) {
|
||||
count--
|
||||
}
|
||||
|
||||
if isVowel(s[i]) {
|
||||
count++
|
||||
}
|
||||
|
||||
maxCount = max(maxCount, count)
|
||||
}
|
||||
|
||||
return maxCount
|
||||
}
|
Loading…
Reference in a new issue