Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.
Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.
Input: s = "cscode", k = 3
Output: 2
Explanation: "ode" contain 2 vowels.
Constraints:

Contents

In this approach, we are going to apply sliding window technique using two pointers left and right, find the total number of vowels found in current window of length k, and keep track of maximum number of vowels found in all substrings of length k.

Implementation steps:
public class MaximumNumberOfVowelsInSubstringOfGivenLength { static int maxVowels(String s, int k) { int left = 0; int right =0; int vowels = 0; int maxVowels = 0; while(right < s.length()) { char current = s.charAt(right); if(isVowel(current)) { vowels++; } while(right-left+1>k){ char leftChar = s.charAt(left); if(isVowel(leftChar)) { vowels--; } left++; } maxVowels = Math.max(maxVowels, vowels); right++; } return maxVowels; } static boolean isVowel(char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'; } public static void main(String[] args) { System.out.println(maxVowels("abciiidef", 3)); System.out.println(maxVowels("aeiou", 2)); System.out.println(maxVowels("cscode", 3)); } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time, where n is the length of input string s.
Space complexity: O(1).

Above implementations source code can be found at GitHub link for Java code