Leetcode 205 - Isomorphic strings

Note:

  • Convert both strings into patterns like ABB.
  • Compare if their patterns are the same.

Question:

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

Two strings s and t 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
2
Input: s = "egg", t = "add"
Output: true

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function(s, t) {
let map1 = new Map();
let pattern1 = '';
let map2 = new Map();
let pattern2 = '';
for (const char of s) {
if (!map1.has(char)) {
const letter = String.fromCharCode(65 + map1.size);
map1.set(char, letter);
pattern1 += letter;
} else {
pattern1 += map1.get(char);
}
}
for (const char of t) {
if (!map2.has(char)) {
const letter = String.fromCharCode(65 + map2.size);
map2.set(char, letter);
pattern2 += letter;
} else {
pattern2 += map2.get(char);
}
}
return pattern1 === pattern2;
};