• 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;
    
        
    
       
            
           
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
  • 相关阅读:
    基于ssm+Vue音乐播放器管理系统java源码
    24届近3年中国矿业大学自动化考研院校分析
    C++ 文件相关操作API说明
    VQA的应用(调研)
    Java中的自定义异常
    NFTScan 正式上线 TON NFTScan 浏览器!
    RISC-V反汇编调试记录分享
    分布式限流不会用?一个注解简单搞定
    SQL的函数
    React(10)-组件通信(important)
  • 原文地址:https://blog.csdn.net/weixin_43327597/article/details/127465266