加载中...
2062-统计字符串中的元音子字符串(Count Vowel Substrings of a String)
发表于:2021-12-03 | 分类: 简单
字数统计: 640 | 阅读时长: 3分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/count-vowel-substrings-of-a-string

英文原文

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"

Example 4:

Input: word = "bbaeixoubb"
Output: 0
Explanation: The only substrings that contain all five vowels also contain consonants, so there are no vowel substrings.

 

Constraints:

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

中文题目

子字符串 是字符串中的一个连续(非空)的字符序列。

元音子字符串 由元音('a''e''i''o''u')组成的一个子字符串,且必须包含 全部五种 元音。

给你一个字符串 word ,统计并返回 word元音子字符串的数目

 

示例 1:

输入:word = "aeiouu"
输出:2
解释:下面列出 word 中的元音子字符串(斜体加粗部分):
- "aeiouu"
- "aeiouu"

示例 2:

输入:word = "unicornarihan"
输出:0
解释:word 中不含 5 种元音,所以也不会存在元音子字符串。

示例 3:

输入:word = "cuaieuouac"
输出:7
解释:下面列出 word 中的元音子字符串(斜体加粗部分):
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"

示例 4:

输入:word = "bbaeixoubb"
输出:0
解释:所有包含全部五种元音的子字符串都含有辅音,所以不存在元音子字符串。

 

提示:

  • 1 <= word.length <= 100
  • word 仅由小写英文字母组成

通过代码

高赞题解

func countVowelSubstrings(word string) (ans int) {
	for _, s := range strings.FieldsFunc(word, func(r rune) bool { return !strings.ContainsRune("aeiou", r) }) { // 分割出仅包含元音的字符串
		cnt := ['v']int{}
		l := 0
		for _, ch := range s {
			cnt[ch]++
			for cnt[s[l]] > 1 { // 双指针,仅当该元音个数不止一个时才移动左指针
				cnt[s[l]]--
				l++
			}
			if cnt['a'] > 0 && cnt['e'] > 0 && cnt['i'] > 0 && cnt['o'] > 0 && cnt['u'] > 0 { // 必须包含全部五种元音
				ans += l + 1
			}
		}
	}
	return
}

统计信息

通过次数 提交次数 AC比率
4927 8181 60.2%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
2060-同源字符串检测(Check if an Original String Exists Given Two Encoded Strings)
下一篇:
2063-所有子字符串中的元音(Vowels of All Substrings)
本文目录
本文目录