• 890. 查找和替换模式


    890. 查找和替换模式

    你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配

    如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。

    (回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)

    返回 words 中与给定模式匹配的单词列表。

    你可以按任何顺序返回答案。

    示例:

    输入:words = [“abc”,“deq”,“mee”,“aqq”,“dkd”,“ccc”], pattern = “abb”
    输出:[“mee”,“aqq”]
    解释:
    “mee” 与模式匹配,因为存在排列 {a -> m, b -> e, …}。
    “ccc” 与模式不匹配,因为 {a -> c, b -> c, …} 不是排列。
    因为 a 和 b 映射到同一个字母。

    这题我做的复杂了,不建议学习,解题代码如下:

    /**
     * Note: The returned array must be malloced, assume caller calls free().
     */
    
    bool patternr(char *wordt,char *pattern){
         char word[strlen(wordt)+1];
         strcpy(word,wordt);
         word[strlen(wordt)]='\0';
      
        if(strlen(pattern)!=strlen(word)){
            return false;
        }
      //  printf("--%s ",word);
       
        int r[26],r2[26];
        for(int i=0;i<26;i++){
            r[i]=1;
            r2[i]=1;
        }
           int rz[strlen(word)];
        for(int i=0;i<strlen(word);i++){
             rz[i]=1;
    
        }
    
        for(int i=0;pattern[i]!='\0';i++){
          
            if(pattern[i]!=word[i]){
    
            
                    if(pattern[i]!=word[i]&&r[pattern[i]-'a']==0){
                        return false;
    
                    }
                     if(pattern[i]!=word[i]&&r2[word[i]-'a']==0){
                        return false;
    
                    }
                     r2[word[i]-'a']=0;
                
    
                    if(pattern[i]!=word[i]&&r[pattern[i]-'a']==1||r2[word[i]-'a']==1){
                        char ch=word[i];
                      
                        for(int j=i;word[j]!='\0';j++){
                            if(word[j]==ch&&rz[j]==1){
                                word[j]=pattern[i];
                                if(word[j]!=pattern[j]){
                                         return false;
                                 }
                                 rz[j]=0;
                            }
                            
                        }
                       
                    }
                    else{
                        return false;
                    }
    
                }
                if(word[i]==pattern[i]&&rz[i]==1){
                      r2[word[i]-'a']=0;
                    
                }
               
               
                  r[pattern[i]-'a']=0;
                 
                
             }
        return true;
          
    }
    
    char ** findAndReplacePattern(char ** words, int wordsSize, char * pattern, int* returnSize){
       int size=0;
        char ** re=(char **)malloc(sizeof(char *)*wordsSize);
    
        for(int i=0;i<wordsSize;i++){
             //  printf("|| ",i);
            if(patternr(words[i],pattern)){
                  
             
                re[size]=(char *)malloc(sizeof(char)*(strlen(pattern)+1));
               
               strcpy(re[size],words[i]);
              
               size++;
            }
            
        }
        *returnSize=size;
        
        
      
    
    
       
      
         return re;
    }
    
    
    
    
    • 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
    • 101
    • 102
    • 103
    • 104
    • 105
  • 相关阅读:
    OpenHarmony 串口服务访问
    UVa658 It’s not a Bug, it’s a Feature!(Dijkstra)
    数据合并与对比
    最新Cocos Creator 3.x 如何动态修改3D物体的透明度
    低价链接,平台投诉,你真的会吗
    Linux使用find命令查找文件
    本地搭建靶站进行漏洞复现和防御(SQL注入、文件上传、XSS漏洞的多种形式)
    MacOS原版镜像iso下载
    VUE模板编译的实现原理
    CentOS-7安装clickhouse并允许其他主机登录
  • 原文地址:https://blog.csdn.net/weixin_43327597/article/details/126518374