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

Example 1:
Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.

Example 2:
Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.

Example 3:
Input: s = "leetcode", k = 3
Output: 2
Explanation: "lee", "eet" and "ode" contain 2 vowels.

Example 4:
Input: s = "rhythms", k = 4
Output: 0
Explanation: We can see that s doesn't have any vowel letters.

Example 5:
Input: s = "tryhard", k = 4
Output: 1

Solution : (Sliding Window)

class Solution
{
public:
    int maxVowels(string s, int k)
    {

        int n = s.length();

        int maxCount = INT_MIN;
        int count = 0;
        for (int i = 0; i < k; i++)
        {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
            {
                count++;
            }
        }

        for (int i = 0; i <= n - k; i++)
        {

            if (count > maxCount)
            {
                maxCount = count;
            }

            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
            {
                count--;
            }

            if (i + k < n)
            {
                if (s[i + k] == 'a' || s[i + k] == 'e' || s[i + k] == 'i' || s[i + k] == 'o' || s[i + k] == 'u')
                {
                    count++;
                }
            }
        }

        return maxCount;
    }
};

Time Complexity: O(k + n-k )

Last updated