class Solution { public: bool wordPattern(string pattern, string str) { map<string, char> word_map; char used[128] = {0}; string word; int pos= 0; str.push_back(‘ ‘); for(int i=0; i<str.size(); ++i) { if(str[i] == ‘ ‘) { //结束条件 if(pos == pattern.size()) { return false; } if(word_map.find(word) == word_map.end()) { if(used[pattern[pos]]) return false; word_map.insert(make_pair(word, pattern[pos])); used[pattern[pos]] = 1; } else { if(word_map[word] != pattern[pos]) return false; } word = ""; pos++; } else { word += str[i]; } } if(pos < pattern.size()) return false; return true; } };
原文地址:https://www.cnblogs.com/randyniu/p/9354889.html
时间: 2024-11-10 15:27:02