class Solution {
public:
bool wordPattern(string pattern, string str) {
// split string
stringstream ss(str);
string buffer;
vector<string> arr;
while (getline(ss, buffer, ' ')) {
arr.push_back(buffer);
}
if (pattern.size() != arr.size()) return false;
unordered_map<char, string> map1;
unordered_map<string, char> map2;
int i = 0;
for (char c : pattern) {
if (map1.count(c) && map1.find(c)->second != arr[i]
|| map2.count(arr[i]) && map2.find(arr[i])->second != c) {
return false;
}
map1.insert(pair<char, string>{c, arr[i]});
map2.insert(pair<string, char>{arr[i], c});
++i;
}
return true;
}
};