**class Solution {
public:
bool wordPattern(string pattern, string str) {
int pindex=0;
int start=0;
int end=0;
int psize=pattern.size();
int ssize=str.size();
map<char,string> m;
while(pindex<psize&&end<ssize)
{
while(end<ssize&&str[end]!=' ')
end++;
string strtemp=str.substr(start,end-start);
char ctemp=pattern[pindex];
if(m.find(ctemp)==m.end())
{
for(auto it=m.begin();it!=m.end();it++)
{
if(it->second==strtemp)
return false;
}
m[ctemp]=strtemp;
}
else
{
if(m[ctemp]!=strtemp)
return false;
}
pindex++;
end++;
start=end;
}
if(pindex<psize||end<ssize)
return false;
else
return true;
}
};**
290. Word Pattern
最新推荐文章于 2017-03-11 22:57:28 发布