Skip to content

Instantly share code, notes, and snippets.

@cfrank
Created June 1, 2023 18:21
Show Gist options
  • Save cfrank/e3bf8f1f889c067b3b6d6fb237c99e29 to your computer and use it in GitHub Desktop.
Save cfrank/e3bf8f1f889c067b3b6d6fb237c99e29 to your computer and use it in GitHub Desktop.
1456. Maximum Number of Vowels in a Substring of Given Length
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
/**
* @param {string} str
* @param {number} frameSize
* @return {number}
*/
var maxVowels = function(str, frameSize) {
// Given a char return true if it's a vowel
const isVowel = (char) => VOWELS.includes(char);
const arr = Array.from(str);
let currentCount = arr.slice(0, frameSize).filter(isVowel).length;
let maxVowelsCount = currentCount;
for (let i = 1; i <= arr.length - frameSize; ++i) {
const leftSide = arr[i - 1];
const rightSide = arr[i + (frameSize - 1)];
if (isVowel(leftSide)) {
currentCount--;
}
if (isVowel(rightSide)) {
currentCount++;
}
if (currentCount > maxVowelsCount) {
maxVowelsCount = currentCount;
}
}
return maxVowelsCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment