# 题目

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,

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

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

e <——> a
g <——> d
g <——> d

# AC代码

class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.size() != t.size())
return false;
//求两个字符串的长度
int len = s.size();

//首先验证字符串s—>t的映射
unordered_map<char, char> um;
for (int i = 0; i < len; ++i)
{
auto pos = um.find(s[i]);
if (pos == um.end())
um.insert({ s[i], t[i] });
else{
if ((*pos).second != t[i])
return false;
}//else
}//for

//再验证字符串t—>s的映射
um.clear();
for (int i = 0; i < len; ++i)
{
auto pos = um.find(t[i]);
if (pos == um.end())
um.insert({ t[i], s[i] });
else{
if ((*pos).second != s[i])
return false;
}//else
}//for
return true;
}
};

GitHub测试程序源码

#### lintcode(647)Substring Anagrams

2017-05-13 09:58:47

#### 387. First Unique Character in a String [easy] (Python)

2016-08-31 19:15:49

#### 字符串同构问题——leetcode205/leetcode290

2016-12-20 20:50:32

#### 205. Isomorphic Strings [easy] (Python)

2016-06-15 14:02:47

#### LeetCode OJ 205 Isomorphic Strings

2015-05-03 13:49:11

#### LeetCode 205:Isomorphic Strings

2015-05-08 23:06:25

#### 【LeetCode】205 Isomorphic Strings （c++实现）

2015-07-22 10:29:55

#### leetcode 205: Isomorphic Strings

2015-04-30 04:54:59

#### 【LeetCode-205】Isomorphic Strings(C++)

2016-09-09 22:40:50

#### leetcode_c++：哈希： Isomorphic Strings(205)

2016-07-10 14:18:42