# LEETCODE 1456. Maximum Number of Vowels in a Substring of Given Length 解题思路分析

```输入：s = "abciiidef", k = 3

```输入：s = "aeiou", k = 2

```输入：s = "leetcode", k = 3

```输入：s = "rhythms", k = 4

```输入：s = "tryhard", k = 4

• `1 <= s.length <= 10^5`
• `s` 由小写英文字母组成
• `1 <= k <= s.length`

```public int maxVowels(String s, int k) {
int count=0;
char[] arr=s.toCharArray();
// 确定下标0到k-1范围内元音个数
for(int i=0;i<k;i++){
if(arr[i]=='a'||arr[i]=='e'||arr[i]=='i'
||arr[i]=='o'||arr[i]=='u') count++;
}
int max=count; // 返回结果
// 不断的向右移动区间
for(int i=k;i<s.length();i++){
// 如果被移入的元素是元音
if(arr[i]=='a'||arr[i]=='e'||arr[i]=='i'
||arr[i]=='o'||arr[i]=='u') count++; // 区间内元音数加一
// 如果被移出的元素是元音
if(arr[i-k]=='a'||arr[i-k]=='e'||arr[i-k]=='i'
||arr[i-k]=='o'||arr[i-k]=='u') count--; // 区间内元音数减一
// 更新最大区间内元音数
max=Math.max(max,count);
}
return max;
}```

Runtime: 8 ms, faster than 98.27% of Java online submissions for Maximum Number of Vowels in a Substring of Given Length.

Memory Usage: 40 MB, less than 100.00% of Java online submissions for Maximum Number of Vowels in a Substring of Given Length.