Maximum Number of Vowels in a Substring of Given Length LeetCode solution in Java
1class Solution {2public int maxVowels(String s, int k) {3int n = s.length();4int maxVowels = 0;5int count = 0;67int[] vowels = new int[128];8vowels['a'] = 1;9vowels['e'] = 1;10vowels['i'] = 1;11vowels['o'] = 1;12vowels['u'] = 1;1314for (int i = 0; i < k; i++) {15count += vowels[s.charAt(i)];16}1718maxVowels = count;19for (int i = k; i < n; i++) {20count += vowels[s.charAt(i)] - vowels[s.charAt(i - k)];21maxVowels = Math.max(maxVowels, count);22// System.out.println(Arrays.toString(vowels));23if (maxVowels == k) {24return maxVowels;25}26}27return maxVowels;28}29}