leetcode 哈希表专题-Word Pattern

原文链接:https://leetcode.com/problems/word-pattern/

原文题目:

Given a pattern and a string str, find if str follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.

Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.

我用的是C语言的实现方法:

bool wordPattern(char* pattern, char* str) {
    int temp[26] = {0};
    if(pattern == NULL || str == NULL || *pattern == '\0' || *str == '\0')
        return false;
    for(int i = 0 ; i < 26 ;  i++ )
    {   
        temp[i] = -1;

    }
    int i = 0;
    int strLength = 0;
    char * saveOfIp[26];//新建一个指针数组
    int inset = 0;
    while(*(pattern + i) != '\0')
    {
        if(strLength != 0)
        {
            if(*(str + strLength -1 )  == '\0')
               return false;
        }
        int weight = 0;
        while(*(str +strLength + weight) != ' ' &&  *(str +strLength + weight) != '\0')
        {
             weight ++;
        }
        char *saveChars = (char *)malloc(weight + 1);
        if(saveChars == NULL)
           printf("error in the malloc");
       if( memcpy(saveChars,str+strLength,weight) == NULL)
            printf("error in the memcpy");
        strLength = strLength + ( weight + 1 );
        *(saveChars + weight ) = '\0';//字符串要加结尾
        char *every;
        char everyTemp;
        everyTemp = *(pattern +i) - 'a';//提取出每次的pattern一个字符用以比较
        if(temp[everyTemp] == -1)
        {
            int j = 0; 
            while(j<26 )
            {
                if(temp[j] == -1)
                {
                       j++;
                       continue;
                }

                if(strcmp(saveOfIp[j],saveChars) == 0)
                    return false;

                j++;
            }
            temp[everyTemp] = 1;//将结果存在哈希表里
            saveOfIp[everyTemp] = (char *)malloc(weight +1);//字符串要加结尾,所以要weight+1
             memcpy(saveOfIp[everyTemp],saveChars,weight + 1);//复制进一个数组保存
        }
        else
        {
            if(strcmp(saveOfIp[everyTemp],saveChars) != 0)
                return false;
        }
        i++;

    }
    if(*(str + strLength - 1)  != '\0')
        return false;
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值