类似205题一样,记录当前字符(串)上一次出现的索引
class Solution
{
public:
bool wordPattern(string pattern, string str)
{
unordered_map<char, int> p2i;
unordered_map<string, int> s2i;
istringstream in(str);
int i = 0, n = pattern.size();
for (string curWord; in >> curWord; ++i)
{
if (i == n || p2i[pattern[i]] != s2i[curWord])
return false;
p2i[pattern[i]] = i + 1;
s2i[curWord] = i + 1;
}
return i == n;
}
};