力扣日常切题
1.统计字符串中的元音子字符串
题解:三重循环暴力
AC代码如下:
class Solution {
public:
int countVowelSubstrings(string word) {
int sum=0;
for(int i=0;i<word.size();i++){
for(int j=i+4;j<word.size();j++)
{ int a=0,e=0,s=0,o=0,u=0,flag=0;
for(int k=i;k<=j;k++)
{
if(word[k]=='a') a=1;
else if(word[k]=='e') e=1;
else if(word[k]=='i') s=1;
else if(word[k]=='o') o=1;
else if(word[k]=='u') u=1;
else
{flag=1; break;}
}
if(a+e+s+o+u==5&&flag==0)
sum++;
}
}
return sum;
}
};
2.所有子字符串中的元音
AC代码如下:
class Solution {
public:
long long countVowels(string word) {
long long sum=0;
for(int i=0;i<word.size();i++)
{
if(word[i]=='a'||word[i]=='e'||word[i]=='i'||word[i]=='o'||word[i]=='u')
sum+=(i+1)*(word.size()-i);
}
return sum;
}
};