重点:hash_table
map方式(时间52ms):
<span style="font-size:14px;">class Solution {
public:
bool isIsomorphic(string s, string t) {
map<char, char> flag;
map<char, bool> bmatch;
int i;
for(i = 0;i<s.size() && i < t.size();i++)
{
if(flag.find(s[i]) == flag.end())
{
if(bmatch.find(t[i]) == bmatch.end())
{
flag[s[i]] = t[i];
bmatch[t[i]] = true;
continue;
}
return false;
}
if(flag[s[i]] != t[i])
return false;
}
if(i < s.size() || i < t.size())
return false;
return true;
}
};</span>
hash方法:(时间8ms)
<span style="font-size:14px;">class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.size() != t.size())
return false;
int flag[300];
std::fill(flag, flag+300, -1);
bool bmatch[300] = {false};
for(int i = 0;i<s.size();i++)
{
if(flag[s[i]] == -1)
{
if(!bmatch[t[i]])
{
flag[s[i]] = t[i];
bmatch[t[i]] = true;
continue;
}
return false;
}
if(flag[s[i]] != t[i])
return false;
}
return true;
}
};
</span>