【leetcode】290单词规律 | hash | 双射 | SplitString

13 篇文章 1 订阅

在这里插入图片描述

题目

给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。

这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。

示例1:

输入: pattern = “abba”, str = “dog cat cat dog”
输出: true

示例 2:

输入:pattern = “abba”, str = “dog cat cat fish”
输出: false

示例 3:

输入: pattern = “aaaa”, str = “dog cat cat dog”
输出: false

示例 4:

输入: pattern = “abba”, str = “dog dog dog dog”
输出: false

说明:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。

题解

看到题目第一反应就是map,注意一下这里的对应关系是双射,就可以上手码代码了

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<char,string> mp1;
        unordered_map<string,char> mp2;
        vector<string> strs;
        SplitString(s,strs," ");
        if(strs.size() != pattern.size()){
            return false;
        }
        for(int i = 0; i != pattern.size(); i++){
            if(mp1.count(pattern[i]) == 0){
                mp1[pattern[i]] = strs[i];
            }
            else if(mp1.count(pattern[i]) == 1){
                if(mp1[pattern[i]] != strs[i]){
                    return false;
                }
            }
        }
        for(int i = 0; i != strs.size(); i++){
            if(mp2.count(strs[i]) == 0){
                mp2[strs[i]] = pattern[i]; 
            }
            else if(mp2.count(strs[i]) == 1){
                if(mp2[strs[i]] != pattern[i]){
                    return false;
                }
            }
        }
        return true;
    }
    void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){
        std::string::size_type pos1, pos2;
        pos2 = s.find(c);
        pos1 = 0;
        while(std::string::npos != pos2){
            v.push_back(s.substr(pos1, pos2-pos1));
            pos1 = pos2 + c.size();
            pos2 = s.find(c, pos1);
        }
        if(pos1 != s.length())
            v.push_back(s.substr(pos1));
        }
};

找到了一个集锦C++中实现字符串split的博客C++常见问题:字符串分割函数split宝藏博主哇

官方题解

思路相似,但人家写得就是比我有美感多了

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        unordered_map<string, char> str2ch;
        unordered_map<char, string> ch2str;
        int m = str.length();
        int i = 0;
        for (auto ch : pattern) {
            if (i >= m) {
                return false;
            }
            int j = i;
            while (j < m && str[j] != ' ') j++;
            const string &tmp = str.substr(i, j - i);
            if (str2ch.count(tmp) && str2ch[tmp] != ch) {
                return false;
            }
            if (ch2str.count(ch) && ch2str[ch] != tmp) {
                return false;
            }
            str2ch[tmp] = ch;
            ch2str[ch] = tmp;
            i = j + 1;
        }
        return i >= m;
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/word-pattern/solution/dan-ci-gui-lu-by-leetcode-solution-6vqv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 时间复杂度:O(n+m),其中 n 为 pattern 的长度,m 为 str 的长度。插入和查询哈希表的均摊时间复杂度均为 O(n+m)。每一个字符至多只被遍历一次。

  • 空间复杂度:(n+m),其中 n 为 pattern 的长度,m 为 str 的长度。最坏情况下,我们需要存储pattern 中的每一个字符和 str 中的每一个字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值