Created
June 1, 2023 18:21
-
-
Save cfrank/e3bf8f1f889c067b3b6d6fb237c99e29 to your computer and use it in GitHub Desktop.
1456. Maximum Number of Vowels in a Substring of Given Length
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
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