Skip to content

Commit 3ca0383

Browse files
authored
1456. Maximum Number of Vowels in a Substring of Given Length
1 parent 212954a commit 3ca0383

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
fun maxVowels(s: String, k: Int): Int {
3+
val vowels = setOf('a', 'e', 'i', 'o', 'u')
4+
var maxCount = 0
5+
var windowVowels = 0
6+
7+
for (i in s.indices) {
8+
if (i >= k) {
9+
if (s[i - k] in vowels) {
10+
windowVowels--
11+
}
12+
}
13+
if (s[i] in vowels) {
14+
windowVowels++
15+
}
16+
maxCount = maxOf(maxCount, windowVowels)
17+
}
18+
return maxCount
19+
}
20+
fun maxVowels1(s: String, k: Int): Int {
21+
val vowels = setOf('a', 'e', 'i', 'o', 'u')
22+
val length = s.length
23+
if (k > length) return 0
24+
var maxCount = 0
25+
for(i in 0..(length-k)){
26+
val subString = s.substring(i, i+k)
27+
val vowelsCount = subString.count{it in vowels}
28+
maxCount = maxOf(maxCount, vowelsCount)
29+
}
30+
return maxCount
31+
}
32+
}

0 commit comments

Comments
 (0)