lc 290. 单词规律

  1. 离散数学中双射的概念(映射+单射+满射)
  2. C++中的stringstream
  3. C++中的正则表达式也可以用来提取单词
class Solution {
public:
    bool wordPattern(string pattern, string s) {

        vector<string> words;
        stringstream ss(s);
        string word;
        while(ss >> word) words.push_back(word);

        if(words.size() != pattern.size()) return false;

        unordered_map<char, string> pattern_to_words;
        unordered_map<string, char> words_to_pattern;

        for(int i = 0; i < pattern.size(); i++) {
            if(pattern_to_words.count(pattern[i]) && pattern_to_words[pattern[i]] != words[i]) return false;
            pattern_to_words[pattern[i]] = words[i];

            if(words_to_pattern.count(words[i]) && words_to_pattern[words[i]] != pattern[i]) return false;
            words_to_pattern[words[i]] = pattern[i];
        }

        return true;
    }
};
全部评论

相关推荐

给🐭🐭个面试机会吧:我boss直聘天天有家教跟我打招呼😓
点赞 评论 收藏
分享
神哥了不得:放平心态,再找找看吧,主要现在计算机也变卷了,然后就比较看学历了,之前高中毕业你技术强,都能找到工作的
点赞 评论 收藏
分享
02-18 17:30
腾讯_TEG_技术
多刷**&nbsp;背八股&nbsp;刷面经&nbsp;项目话术准备好&nbsp;不会差的!!!后台看到好多小伙伴们都出现其中一个环节的错误,,,可惜了抓紧机会吧&nbsp;有的是hc&nbsp;但缺的就是稍微用心的人
野猪不是猪🐗:多刷星星,背八股背话术,真的能过你们?对一个个没实习过的学生狂问场景题设计题和底层深挖,别以为我不知道一边说缺人还一边各种kpi面
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务