---
title: "1456. Maximum Number of Vowels in a Substring of Given Length"
url: "https://laigary.com/interview/coding/1456-maximum-number-of-vowels-in-a-substring-of-given-length"
type: "note"
section: "coding"
date: "2024-03-09"
updated: "2024-03-09"
tags: ["Sliding Window"]
---

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

[1456\. Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/)

```python
class Solution:
    def maxVowels(self, s: str, k: int) -> int:
        
        vowels = {"a", "e", "i", "o", "u"}

        count = 0
        for i in range(k):
            if s[i] in vowels:
                count += 1

        maxLength = count

        for i in range(k, len(s)):
            if s[i] in vowels:
                count += 1
            if s[i - k] in vowels:
                count -= 1
            maxLength = max(maxLength, count)

        return maxLength
```
