当前位置:网站首页>LeetCode简单题之统计字符串中的元音子字符串
LeetCode简单题之统计字符串中的元音子字符串
2022-04-23 07:02:00 【·星辰大海】
题目
子字符串 是字符串中的一个连续(非空)的字符序列。
元音子字符串 是 仅 由元音(‘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 仅由小写英文字母组成
来源:力扣(LeetCode)
解题思路
遍历字符串,从当前字符串开始向右开始枚举,如果一路都能遇到不重复或者重复的元音字母并且种类能达到5种就算一个字串,中途断掉则不算直接跳出当前枚举,进行下一次枚举,从当前字符的下一个字符开始。
class Solution:
def countVowelSubstrings(self, word: str) -> int:
count,alpha=0,{
'a','e','i','o','u'}
for i in range(len(word)):
if word[i] in alpha:
temp={
word[i]}
else:
continue
for j in range(i+1,len(word)):
if word[j] in alpha:
temp.add(word[j])
else:
break
if len(temp)==5:
count+=1
return count
版权声明
本文为[·星辰大海]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_18560985/article/details/124356917
边栏推荐
猜你喜欢
Construction of middleman environment mitmproxy
BUFFCTF文件中的秘密1
nn.Module类的讲解
Feign source code analysis
Briefly describe the hierarchical strategy of memory
Distributed service governance Nacos
利用Js实现一个千分位
Brief description of CPU
Why are there 1px problems? How?
[programming practice / embedded competition] learning record of embedded competition (II): picture streaming based on TCP
随机推荐
Why are there 1px problems? How?
Research on system and software security (5)
Flutter之Provider共享数据的两种方式
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
【Appium】测试时遇到手机内嵌H5页面的切换问题
利用Js实现一个千分位
Penetration test interview collection -- HVV---
編譯原理題-帶答案
Go语学习笔记 - 异常处理 | 从零开始Go语言
C outputs a two-dimensional array with the following characteristics.
php生成短链接:将数字转成字母,将字母转成数字
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
Ubuntu安装Mysql并查询平均成绩
简述CPU
访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
Mobile terminal layout (3D conversion, animation)
Hierarchical output binary tree
Comparison of indoor positioning methods of several intelligent robots
3C装配中的机械臂运动规划
LeetCode15. 三数之和