Algorithm
LeetCode 75
Maximum Number of Vowels in a Substring of Given Length

1456. Maximum Number of Vowels in a Substring of Given Length

Tags

  • String
  • Sliding Window

Link

https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/?envType=study-plan-v2&envId=leetcode-75 (opens in a new tab)

Question

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.
Constraints:
  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
  • 1 <= k <= s.length

Answer

JavaScript

/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var maxVowels = function (s, k) {
  let left = 0,
    right = 0,
    count = 0,
    maxCount = 0;
  const isVowal = (char) => {
    return (
      char == "a" || char == "e" || char == "i" || char == "o" || char == "u"
    );
  };
 
  while (right < s.length) {
    // console.log(left, right, count)
    if (isVowal(s[right])) count++;
    if (right >= k) {
      if (isVowal(s[left])) count--;
      left++;
    }
    right++;
    maxCount = Math.max(count, maxCount);
  }
  return maxCount;
};
/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var maxVowels = function (s, k) {
  let cnt = (vcnt = left = right = 0),
    maxi = -Infinity;
 
  while (right < s.length) {
    if (isVowel(s[right])) vcnt++;
 
    if (cnt >= k) {
      if (isVowel(s[left])) vcnt--;
      cnt--;
      left++;
    }
 
    cnt++;
    if (cnt == k) maxi = Math.max(maxi, vcnt);
    right++;
  }
 
  function isVowel(char) {
    return (
      char === "a" ||
      char === "e" ||
      char === "i" ||
      char === "o" ||
      char === "u"
    );
  }
 
  return maxi;
};
/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var maxVowels = function (s, k) {
  if (k > s.length) return 0;
 
  const isVowel = (char) => {
    return (
      char === "a" ||
      char === "e" ||
      char === "i" ||
      char === "o" ||
      char === "u"
    );
  };
 
  let maxCount = 0;
  let count = 0;
 
  let L = 0;
  for (let R = 0; R < s.length; R++) {
    if (isVowel(s[R])) count++;
    if (R - L + 1 > k) {
      if (isVowel(s[L])) {
        count--;
      }
      L++;
    }
    maxCount = Math.max(maxCount, count);
  }
 
  return maxCount;
};
/**
 * @param {string} s
 * @param {number} k
 * @return {number}
 */
var maxVowels = function (s, k) {
  let ans = 0;
  let count = 0;
 
  // Count vowels in the first window
  for (let i = 0; i < k; i++) {
    if (isVowel(s[i])) {
      count++;
    }
  }
  ans = Math.max(ans, count);
 
  // Slide the window and update the count
  for (let i = k; i < s.length; i++) {
    if (isVowel(s[i])) {
      count++;
    }
    if (isVowel(s[i - k])) {
      count--;
    }
    ans = Math.max(ans, count);
  }
 
  return ans;
};
 
function isVowel(c) {
  return c === "a" || c === "e" || c === "i" || c === "o" || c === "u";
}