-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_1456.java
More file actions
29 lines (28 loc) · 828 Bytes
/
Copy pathproblem_1456.java
File metadata and controls
29 lines (28 loc) · 828 Bytes
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
/*
1456. Maximum Number of Vowels in a Substring of Given Length
Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k. */
class Solution {
public int maxVowels(String s, int k) {
String sub_string, vow = "aeiouAEIOU";
int max = 0, count = 0;
for(int i = 0;i<k;i++){
if(vow.indexOf(s.charAt(i))>-1){
max++;
}
}
count = max;
for(int i = k;i<s.length();i++){
if(vow.indexOf(s.charAt(i)) > -1){
count++;
}
if(vow.indexOf(s.charAt(i-k))> -1){
count--;
}
max = max<count? count:max;
if(max == k){
return max;
}
}
return max;
}
}