leetcode_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.

题目的意思是要我们判断pattern和str是否匹配,匹配的条件是pattern中的字母与str中的单词是否能一一对应,以pattern = “abba”, str = “dog cat cat dog”为例,a与dog对应,b与cat对应。

解题思路:建立pattern哈希表,每当截取一个子串,就将其与哈希表内的字符串比较。

代码

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        vector<string> hashTable(26,"");                //pattern哈希,例如a->0->dog,b->1->cat
        int indexOfParttern = 0;                        //pattern下标
        int start = 0;                                  //用于截取子串,子串的起始下标
        int end = 0;                                    //用于截取子串,子串的结束下标+1
        int count = 0;
        string sub = str;

        while((end = sub.find_first_of(' '))!=-1)
        {
            sub = sub.substr(0, end);

            int hashPos = pattern[indexOfParttern++]-'a';
            if (hashTable[hashPos].empty())
            {
                //如果即将插入哈希表的值在哈希表已经存在则返回false
                for (int i = 0; i != hashTable.size(); ++i)
                {
                    if (hashTable[i] == sub)
                    {
                        return false;
                    }
                }
                hashTable[hashPos] = sub;
            }
            else
            {
                if (hashTable[hashPos]!= sub)
                {
                    return false;
                }
            }

            start += end+1;
            sub = str.substr(start, str.length()-start);
            ++count;
        }

        //若模式串字母个数与str的子串个数不同则false
        if (pattern.length() != indexOfParttern + 1)
        {
            return false;
        }

        //判断最后一个子串
        int hashPos = pattern[indexOfParttern++]-'a';
        if (hashTable[hashPos].empty())
        {
            for (int i = 0; i != hashTable.size(); ++i)
            {
                if (hashTable[i] == sub)
                {
                    return false;
                }
            }
            hashTable[hashPos] = sub;
        }
        else
        {
            if (hashTable[hashPos]!= sub)
            {
                return false;
            }
        }
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

alw_123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值