leetcode-1456. 定长子串中元音的最大数目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public int maxVowels(String s, int k) {
Set<Character> set = new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');

int ans = 0;
int x = 0;
int i = 0;
int j = 0;
int n = s.length();
while(j < n){
if(set.contains(s.charAt(j))){
x ++;
}
j++;
while(j - i == k){
ans = Math.max(ans, x);
if(set.contains(s.charAt(i))){
x --;
}
i ++;
}
}
return ans;
}
}