您的位置:首页 > 其它

LeetCode205. Isomorphic Strings

2016-01-24 12:51 337 查看

题目链接:

https://leetcode.com/problems/isomorphic-strings/

题目描述:

判断两个字符串s,t是否同构。

s中的字符能被t中对应字符替换。

For example,

Given “egg”, “add”, return true.

Given “foo”, “bar”, return false.

Given “paper”, “title”, return true.

题目分析:

这道题跟LeetCode290基本一样

http://blog.csdn.net/codetz/article/details/50569138

建立两个map防止多对一情况,形成一一对应关系。

代码:

class Solution {
public:
bool isIsomorphic(string s, string t) {
if(t.size()!=s.size()){
return false;
}
int len=s.size();
map<char,char> m1;
map<char,char> m2;
for(int i=0;i<len;i++){
if(m1.find(s[i])==m1.end() && m2.find(t[i])==m2.end()){
m1[s[i]]=t[i];
m2[t[i]]=s[i];
}
else if(m1[s[i]]!=t[i] || m2[t[i]]!=s[i]){
return false;
}
}
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode string hash