-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (39 loc) · 1.03 KB
/
Solution.cs
File metadata and controls
40 lines (39 loc) · 1.03 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
namespace LeetCode.Medium.Problem1456{
//1456. Maximum Number of Vowels in a Substring of Given Length
//https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/
/*
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
*/
public class Solution {
public int MaxVowels(string s, int k) {
int ans = 0;
int sum = 0;
for (int i = 0; i < k; i++)
{
if (IsViowel(s[i]))
sum++;
}
ans = sum;
for (int i = k; i < s.Length; i++)
{
if (IsViowel(s[i]))
sum++;
if (IsViowel(s[i - k]))
sum--;
ans = Math.Max(ans, sum);
}
return ans;
}
private bool IsViowel(char k)
{
if (
k == 'a'
|| k == 'e'
|| k == 'i'
|| k == 'o'
|| k == 'u')
return true;
return false;
}
}
}