diff --git a/go/maximum-number-of-vowels-in-a-substring-of-given-length.go b/go/maximum-number-of-vowels-in-a-substring-of-given-length.go new file mode 100644 index 0000000..06aca4d --- /dev/null +++ b/go/maximum-number-of-vowels-in-a-substring-of-given-length.go @@ -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 +}