返回介绍

solution / 1400-1499 / 1456.Maximum Number of Vowels in a Substring of Given Length / README_EN

发布于 2024-06-17 01:03:19 字数 4847 浏览 0 评论 0 收藏 0

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

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def maxVowels(self, s: str, k: int) -> int:
    vowels = set('aeiou')
    t = sum(c in vowels for c in s[:k])
    ans = t
    for i in range(k, len(s)):
      t += s[i] in vowels
      t -= s[i - k] in vowels
      ans = max(ans, t)
    return ans
class Solution {
  public int maxVowels(String s, int k) {
    int t = 0, n = s.length();
    for (int i = 0; i < k; ++i) {
      if (isVowel(s.charAt(i))) {
        ++t;
      }
    }
    int ans = t;
    for (int i = k; i < n; ++i) {
      if (isVowel(s.charAt(i))) {
        ++t;
      }
      if (isVowel(s.charAt(i - k))) {
        --t;
      }
      ans = Math.max(ans, t);
    }
    return ans;
  }

  private boolean isVowel(char c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  }
}
class Solution {
public:
  int maxVowels(string s, int k) {
    int t = 0, n = s.size();
    for (int i = 0; i < k; ++i) t += isVowel(s[i]);
    int ans = t;
    for (int i = k; i < n; ++i) {
      t += isVowel(s[i]);
      t -= isVowel(s[i - k]);
      ans = max(ans, t);
    }
    return ans;
  }

  bool isVowel(char c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  }
};
func maxVowels(s string, k int) int {
  isVowel := func(c byte) bool {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
  }
  t := 0
  for i := 0; i < k; i++ {
    if isVowel(s[i]) {
      t++
    }
  }
  ans := t
  for i := k; i < len(s); i++ {
    if isVowel(s[i]) {
      t++
    }
    if isVowel(s[i-k]) {
      t--
    }
    ans = max(ans, t)
  }
  return ans
}
function maxVowels(s: string, k: number): number {
  function isVowel(c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  }
  let t = 0;
  for (let i = 0; i < k; ++i) {
    if (isVowel(s.charAt(i))) {
      t++;
    }
  }
  let ans = t;
  for (let i = k; i < s.length; ++i) {
    if (isVowel(s.charAt(i))) {
      t++;
    }
    if (isVowel(s.charAt(i - k))) {
      t--;
    }
    ans = Math.max(ans, t);
  }
  return ans;
}
class Solution {
  /**
   * @param String $s
   * @param Integer $k
   * @return Integer
   */
  function isVowel($c) {
    return $c === 'a' || $c === 'e' || $c === 'i' || $c === 'o' || $c === 'u';
  }
  function maxVowels($s, $k) {
    $cnt = 0;
    $rs = 0;
    for ($i = 0; $i < $k; $i++) {
      if ($this->isVowel($s[$i])) {
        $cnt++;
      }
    }
    $rs = $cnt;
    for ($j = $k; $j < strlen($s); $j++) {
      if ($this->isVowel($s[$j - $k])) {
        $cnt--;
      }
      if ($this->isVowel($s[$j])) {
        $cnt++;
      }
      $rs = max($rs, $cnt);
    }
    return $rs;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文