当前位置:网站首页>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
边栏推荐
- AAAI 2022招募讲者啦!!
- 利用sqlmap注入获取网址管理员账号密码
- NFT ecological development of Ignis public chain: unicorn Donation and development of Art
- php高精度计算
- The following program deletes n consecutive words starting from the ith character from the string str
- Samsung, March to the west again
- Research on system and software security (I)
- 一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
- 社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播
- Principle of sentinel integrating Nacos to update data dynamically
猜你喜欢

社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播

thinkphp6+jwt 实现登录验证

一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
![[极客大挑战 2019]Havefun1](/img/8b/b15bf31771d54db25f24d630e64093.png)
[极客大挑战 2019]Havefun1
![BUUCTF [极客大挑战 2019]EasySQL1](/img/ad/afca09bc1da003393319af700e90e3.png)
BUUCTF [极客大挑战 2019]EasySQL1

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

一篇文章看懂变量提升(hoisting)

高精度焊接机械臂定位

简述存储器的分级策略
![[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface](/img/f1/09de53509479a01098d3cf46bf48eb.jpg)
[programming practice / embedded competition] learning record of embedded competition (I): establishment of TCP server and web interface
随机推荐
Concours de compétences en informatique en nuage - - première partie de l'environnement cloud privé openstack
Briefly describe the hierarchical strategy of memory
Jetson Xavier NX (3) bazel mediapipe installation
Hierarchical output binary tree
云计算赛项--2020年赛题基础部分[任务3]
Compiler des questions de principe - avec des réponses
为什么会存在1px问题?怎么解决?
校园转转二手市场源码下载
Face to face summary 2
sql 使用过的查询语句
Construction of middleman environment mitmproxy
多目视觉SLAM
Ignis公链的NFT生态发展:Unicorn.art的捐赠开发之路
智能名片小程序名片详情页功能实现关键代码
RAID0和RAID5的创建和模拟RAID5工作原理
Codeforces Round #784 (Div. 4)
情境领导者-第七章、解决绩效问题
有意思的js 代码
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
社区团购小程序源码+界面diy+附近团长+供应商+拼团+菜谱+秒杀+预售+配送+直播