-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_no_of_Vowels_substring.java
More file actions
50 lines (39 loc) · 1.17 KB
/
Max_no_of_Vowels_substring.java
File metadata and controls
50 lines (39 loc) · 1.17 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// 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'.
// Example 1:
// Input: s = "abciiidef", k = 3
// Output: 3
// Explanation: The substring "iii" contains 3 vowel letters.
// Example 2:
// Input: s = "aeiou", k = 2
// Output: 2
// Explanation: Any substring of length 2 contains 2 vowels.
// Example 3:
// Input: s = "leetcode", k = 3
// Output: 2
// Explanation: "lee", "eet" and "ode" contain 2 vowels.
class Solution {
public int maxVowels(String s, int k) {
int max_vowels = 0;
int vowels = 0;
int start = 0;
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(isVowel(c)){
vowels++;
}
if(i - start+1 > k){
char startChar = s.charAt(start);
if(isVowel(startChar)){
vowels--;
}
start++;
}
max_vowels = Math.max(max_vowels,vowels);
}
return max_vowels;
}
private boolean isVowel(char c){
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
}