Skip to content
LC-1456 Medium LeetCode

1456. Maximum Number of Vowels in a Substring of Given Length

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 60% Topics: String, Sliding Window
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def maxVowels(self, s, k):
        """
        :type s: str
        :type k: int
        :rtype: int
        """
        VOWELS = set("aeiou")
        result = curr = 0
        for i, c in enumerate(s):
            curr += c in VOWELS
            if i >= k:
                curr -= s[i-k] in VOWELS
            result = max(result, curr)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT