problem
多理解理解题意!!!
不过博主还是不理解,应该比较的是单词的首字母和pattern的顺序是否一致。疑惑!知道的可以分享一下下哈~
之前理解有误,应该是pattern中的每个字符和某个单词的一一映射关系。
class Solution { public: bool wordPattern(string pattern, string str) { map<char, int> p2i; map<string, int> s2i; int i = 0; istringstream in(str); for(string word; in>>word; ++i) { if(p2i.find(pattern[i])!=p2i.end()||s2i.find(word)!=s2i.end())//err. { if(p2i[pattern[i]]!=s2i[word]) return false; } else { p2i[pattern[i]] = s2i[word] = i+1;// } } return i==pattern.size(); } };
参考
2. Leetcode_Discuss;
3. GrandYang;
完
完