1、问题
2、算法标签
1、双哈希表
3、通过代码
class Solution {
public:
bool wordPattern(string pattern, string s) {
map<char,string> ans1;
map<string,char> ans2;
int len1=pattern.length();
int len2=s.length();
int p1=0;
int p2=0;
string temp="";
while(p2<=len2){
if(s[p2]!=' '&&p2<len2){
temp+=s[p2];
}
else{
if(ans1.count(pattern[p1])==0){
ans1.insert(make_pair(pattern[p1],temp));
}
else if(ans1[pattern[p1]]!=temp){
return false;
}
if(ans2.count(temp)==0){
ans2.insert(make_pair(temp,pattern[p1]));
}
else if(ans2[temp]!=pattern[p1]){
return false;
}
temp="";
p1++;
}
p2++;
}
if(p1<len1){
return false;
}
return true;
}
};