0290.cpp (631B)
1 class Solution { 2 public: 3 bool wordPattern(string pattern, string s) { 4 vector<string> done(26, ""); 5 unordered_set<string> st; 6 7 int c = 0; 8 istringstream in(s); 9 for (string crnt; in >> crnt; ++c) { 10 if (c >= pattern.size()) return false; 11 int index = pattern[c] - 'a'; 12 if (done[index].empty()) { 13 if (st.count(crnt)) return false; 14 st.insert(done[index] = crnt); 15 } else if (done[index] != crnt) 16 return false; 17 } 18 if (pattern.size() > c) return false; 19 20 return true; 21 } 22 };