- 离散数学中双射的概念(映射+单射+满射)
- C++中的stringstream
- 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; } };