当前位置:网站首页>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
边栏推荐
- 浅谈ES6尾调优化
- The following program deletes n consecutive words starting from the ith character from the string str
- Discussion on ES6 tail tune optimization
- idea:使用easyYapi插件导出yapi接口
- Implementation of promise all
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
- LeetCoed18. 四数之和
- Compiler des questions de principe - avec des réponses
- 一篇文章看懂变量提升(hoisting)
- Jetson Xavier NX (3) bazel mediapipe installation
猜你喜欢

Comparison of indoor positioning methods of several intelligent robots

在MATLAB中快速画圆(给出圆心坐标和半径就能直接画的那种)

CTF attack and defense world brush questions 51-

Go语学习笔记 - 结构体 | 从零开始Go语言

扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...

How to import Excel data in SQL server, 2019 Edition
![BUUCTF [极客大挑战 2019]EasySQL1](/img/ad/afca09bc1da003393319af700e90e3.png)
BUUCTF [极客大挑战 2019]EasySQL1

【无标题】

数据安全问题已成隐患,看vivo如何让“用户数据”重新披甲

Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
随机推荐
数据库之MySQL——基本常用查询命令
BUUCTF [极客大挑战 2019]EasySQL1
Compiler des questions de principe - avec des réponses
Hump naming object
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
Ubuntu安装Mysql并查询平均成绩
青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
Intranet penetration series: dns2tcp of Intranet tunnel
Move layout (Flex layout, viewport label)
渗透测试面试合集---HVV---
访问数据库的时候出现错误 Operation not allowed for a result set of type ResultSet.TYPE_FORWARD_ONLY.详解
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
How to import Excel data in SQL server, 2019 Edition
nn.Module类的讲解
Construction of middleman environment mitmproxy
[Effective Go 中文翻译]函数篇
有意思的js 代码
1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
Why are there 1px problems? How?