# 205. Isomorphic Strings
LeetCode problem link (opens new window)
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
Hint: You may assume both s and t have the same length.
# Approach
The prompt does not specify that the strings are lowercase letters only, so using an array is not suitable. We'll use a map to create a mapping.
Use two maps to save the mapping: from s[i] to t[j] and from t[j] to s[i]. If at any point the mapping does not match, return false.
The C++ code is as follows:
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> map1;
unordered_map<char, char> map2;
for (int i = 0, j = 0; i < s.size(); i++, j++) {
if (map1.find(s[i]) == map1.end()) { // map1 saves s[i] to t[j] mapping
map1[s[i]] = t[j];
}
if (map2.find(t[j]) == map2.end()) { // map2 saves t[j] to s[i] mapping
map2[t[j]] = s[i];
}
// If the mapping does not match, return false immediately
if (map1[s[i]] != t[j] || map2[t[j]] != s[i]) {
return false;
}
}
return true;
}
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Other Language Versions
# Java
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> map1 = new HashMap<>();
Map<Character, Character> map2 = new HashMap<>();
for (int i = 0, j = 0; i < s.length(); i++, j++) {
if (!map1.containsKey(s.charAt(i))) {
map1.put(s.charAt(i), t.charAt(j)); // map1 saves the mapping from s[i] to t[j]
}
if (!map2.containsKey(t.charAt(j))) {
map2.put(t.charAt(j), s.charAt(i)); // map2 saves the mapping from t[j] to s[i]
}
// If mapping cannot be done, return false
if (map1.get(s.charAt(i)) != t.charAt(j) || map2.get(t.charAt(j)) != s.charAt(i)) {
return false;
}
}
return true;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Python
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
default_dict1 = defaultdict(str)
default_dict2 = defaultdict(str)
if len(s) != len(t): return false
for i in range(len(s)):
if not default_dict1[s[i]]:
default_dict1[s[i]] = t[i]
if not default_dict2[t[i]]:
default_dict2[t[i]] = s[i]
if default_dict1[s[i]] != t[i] or default_dict2[t[i]] != s[i]:
return False
return True
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Go
func isIsomorphic(s string, t string) bool {
map1 := make(map[byte]byte)
map2 := make(map[byte]byte)
for i := range s {
if _, ok := map1[s[i]]; !ok {
map1[s[i]] = t[i] // map1 saves the mapping from s[i] to t[j]
}
if _, ok := map2[t[i]]; !ok {
map2[t[i]] = s[i] // map2 saves the mapping from t[i] to s[j]
}
// If mapping cannot be done, return false
if (map1[s[i]] != t[i]) || (map2[t[i]] != s[i]) {
return false
}
}
return true
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# JavaScript
var isIsomorphic = function(s, t) {
let len = s.length;
if(len === 0) return true;
let maps = new Map();
let mapt = new Map();
for(let i = 0, j = 0; i < len; i++, j++){
if(!maps.has(s[i])){
maps.set(s[i],t[j]);// maps saves the mapping from s[i] to t[j]
}
if(!mapt.has(t[j])){
mapt.set(t[j],s[i]);// mapt saves the mapping from t[j] to s[i]
}
// If mapping cannot be done, return false
if(maps.get(s[i]) !== t[j] || mapt.get(t[j]) !== s[i]){
return false;
}
};
return true;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# TypeScript
function isIsomorphic(s: string, t: string): boolean {
const helperMap1: Map<string, string> = new Map();
const helperMap2: Map<string, string> = new Map();
for (let i = 0, length = s.length; i < length; i++) {
let temp1: string | undefined = helperMap1.get(s[i]);
let temp2: string | undefined = helperMap2.get(t[i]);
if (temp1 === undefined && temp2 === undefined) {
helperMap1.set(s[i], t[i]);
helperMap2.set(t[i], s[i]);
} else if (temp1 !== t[i] || temp2 !== s[i]) {
return false;
}
}
return true;
};
2
3
4
5
6
7
8
9
10
11
12
13
14
15