# 205. Isomorphic Strings

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.

Example 1:

``````Input: _s_ = "egg", _t =_"add"
Output: true
``````

Example 2:

``````Input: _s_ = "foo", _t =_"bar"
Output: false
``````

Example 3:

``````Input: _s_ = "paper", _t =_"title"
Output: true
``````

Note:
You may assume both  s and  t have the same length.

``````class Solution {
public:
bool isIsomorphic(string s, string t) {
int m1[256] = {0}, m2[256] = {0}, n = s.size();
for (int i = 0; i < n; ++i) {
if (m1[s[i]] != m2[t[i]]) return false;
m1[s[i]] = i + 1;
m2[t[i]] = i + 1;
}
return true;
}
};
``````

Github 同步地址：

https://github.com/grandyang/leetcode/issues/205

Word Pattern

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

https://leetcode.com/problems/isomorphic-strings/discuss/57796/My-6-lines-solution

https://leetcode.com/problems/isomorphic-strings/discuss/57810/Short-Java-solution-without-maps

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation