返回介绍

solution / 2000-2099 / 2062.Count Vowel Substrings of a String / README_EN

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

2062. Count Vowel Substrings of a String

中文文档

Description

A substring is a contiguous (non-empty) sequence of characters within a string.

A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it.

Given a string word, return _the number of vowel substrings in_ word.

 

Example 1:

Input: word = "aeiouu"
Output: 2
Explanation: The vowel substrings of word are as follows (underlined):
- "aeiouu"
- "aeiouu"

Example 2:

Input: word = "unicornarihan"
Output: 0
Explanation: Not all 5 vowels are present, so there are no vowel substrings.

Example 3:

Input: word = "cuaieuouac"
Output: 7
Explanation: The vowel substrings of word are as follows (underlined):
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"

 

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase English letters only.

Solutions

Solution 1

class Solution:
  def countVowelSubstrings(self, word: str) -> int:
    n = len(word)
    s = set('aeiou')
    return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
class Solution {
  public int countVowelSubstrings(String word) {
    int n = word.length();
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      Set<Character> t = new HashSet<>();
      for (int j = i; j < n; ++j) {
        char c = word.charAt(j);
        if (!isVowel(c)) {
          break;
        }
        t.add(c);
        if (t.size() == 5) {
          ++ans;
        }
      }
    }
    return ans;
  }

  private boolean isVowel(char c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  }
}
class Solution {
public:
  int countVowelSubstrings(string word) {
    int ans = 0;
    int n = word.size();
    for (int i = 0; i < n; ++i) {
      unordered_set<char> t;
      for (int j = i; j < n; ++j) {
        char c = word[j];
        if (!isVowel(c)) break;
        t.insert(c);
        ans += t.size() == 5;
      }
    }
    return ans;
  }

  bool isVowel(char c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
  }
};
func countVowelSubstrings(word string) int {
  ans, n := 0, len(word)
  for i := range word {
    t := map[byte]bool{}
    for j := i; j < n; j++ {
      c := word[j]
      if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
        break
      }
      t[c] = true
      if len(t) == 5 {
        ans++
      }
    }
  }
  return ans
}
function countVowelSubstrings(word: string): number {
  let ans = 0;
  const n = word.length;
  for (let i = 0; i < n; ++i) {
    const t = new Set<string>();
    for (let j = i; j < n; ++j) {
      const c = word[j];
      if (!(c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u')) {
        break;
      }
      t.add(c);
      if (t.size === 5) {
        ans++;
      }
    }
  }
  return ans;
}

Solution 2

class Solution:
  def countVowelSubstrings(self, word: str) -> int:
    s = set('aeiou')
    ans, n = 0, len(word)
    for i in range(n):
      t = set()
      for c in word[i:]:
        if c not in s:
          break
        t.add(c)
        ans += len(t) == 5
    return ans

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

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

发布评论

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