2062. 统计字符串中的元音子字符串-c语言解法
子字符串 是字符串中的一个连续(非空)的字符序列。
元音子字符串 是 仅 由元音(‘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
解释:所有包含全部五种元音的子字符串都含有辅音,所以不存在元音子字符串。
这一题用c语言去做可谓是非常的麻烦,但是博主还是克服困难,解出来了,感兴趣可以学习一下,解题代码如下:
bool judge(char ch){
if(ch=='a'||ch=='i'||ch=='o'||ch=='e'||ch=='u'){
return true;
}
return false;
}
int index_re(char ch){
if(ch=='a'){
return 0;
}
else if(ch=='e'){
return 1;
}
else if(ch=='i'){
return 2;
}
else if(ch=='o'){
return 3;
}
else {
return 4;
}
}
int countVowelSubstrings(char * word){
int len=strlen(word);
int r[len][5];
for(int i=0;i<5;i++){
r[0][i]=0;
}
if(judge(word[0])){
r[0][index_re(word[0])]++;
}
for(int i=1;i<len;i++){
for(int k=0;k<5;k++){
r[i][k]=r[i-1][k];
}
if(judge(word[i])){
r[i][index_re(word[i])]++;
}
}
int count=0;
for(int i=0;i<len-4;i++){
int rz=0;
int j;
for( j=i;j<=i+4;j++){
if(!judge(word[j])){
rz=1;
break;
}
}
j--;
if(rz==0){
while(judge(word[j])){
if(i==0){
if(r[j][0]>0&&r[j][1]>0&&r[j][2]>0&&r[j][3]>0&&r[j][4]>0){
count++;
}
}
else{
if(r[j][0]>r[i-1][0]&&r[j][1]>r[i-1][1]&&r[j][2]>r[i-1][2]&&r[j][3]>r[i-1][3]&&r[j][4]>r[i-1][4]){
count++;
}
}
j++;
if(j==len){
break;
}
}
}
}
return count;
}