From e9e304b2aad3d6daac1f7548e43c12064b10aef9 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Mon, 12 Aug 2024 17:15:28 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB1456.=20Maximum=20Number=20of?= =?UTF-8?q?=20Vowels=20in=20a=20Substring=20of=20Given=20Length=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...f-vowels-in-a-substring-of-given-length.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 go/maximum-number-of-vowels-in-a-substring-of-given-length.go 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 +}