2062. 统计字符串中的元音子字符串-c语言解法

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;

    

   
        
       

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值