Created
August 12, 2023 08:55
-
-
Save ritik-agrawal/f9e40a6410732e5bc55e5daf2baf8337 to your computer and use it in GitHub Desktop.
LeetCode: Max count of vowel is a given string in a given interval.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int maxVowels(String s, int k) { | |
int slen = s.length(); | |
int[] dp = new int[slen]; | |
int vcc = 0; | |
for (int i = 0; i < slen; i++){ | |
if (isVowel(s.charAt(i))){ | |
dp[i] = ++vcc; | |
} else { | |
dp[i] = vcc; | |
} | |
} | |
int ret = 0; | |
for (int i = 0; i < (slen-k+1); i++){ | |
int end = i+k-1; | |
if (i == 0){ | |
ret = Math.max(ret, dp[end]); | |
} else { | |
ret = Math.max(ret, (dp[end] - dp[i-1])); | |
} | |
} | |
return ret; | |
} | |
private boolean isVowel(Character c){ | |
return ( | |
c == 'a' || | |
c == 'e' || | |
c == 'i' || | |
c == 'o' || | |
c == 'u' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Question
Given with a string and an int value
k
. Find the max number of vowels in the intervalk
in the given string.Solution
The above problem has been solved using two approaches as given below
Sliding Window
k
interval.k
to the last index while performingThe above approach beats 77 % of the total submissions with a runtime of 15 ms. (Done by me.)
Dynamic Programming
The above approach beats 97 % of the total solutions submitted with a runtime of 9 ms.