Check if two given strings are isomorphic to each other
isIsomorphic:
A method ingeniously designed to determine the isomorphism between two strings, s and t. It employs two maps to track character mappings from s to t and vice versa, ensuring a bijective relationship—each character from one string must map to a unique character in the other, with no overlaps or mismatches. The function iterates through the strings, checking and establishing mappings. If at any point a character does not map correctly according to previously established rules (indicating a one-to-one correspondence has been violated), the function concludes the strings are not isomorphic, returning false. This method encapsulates a straightforward yet effective approach to verifying the isomorphic nature of string pairs, ensuring every character mapping is both exclusive and consistent across the strings.
1/**
2 * @param {string} s
3 * @param {string} t
4 * @return {boolean}

5 */
6let s = 'egg',
7  t = 'add';
8
9let s1 = 'foo',
10  t1 = 'bar';
11
12let s2 = 'paper',
13  t2 = 'title';
14
15let s3 = 'badc',
16  t3 = 'baba';
17var isIsomorphic = function (s, t) {
18  let m = new Map();
19  let n = new Map();
20  let flag1 = true;
21  for (let i = 0; i < s.length; i++) {
22    if ((m.has(s[i]) && m.get(s[i]) !== t[i]) || (n.has(t[i]) && n.get(t[i]) !== s[i])) {
23      flag1 = false;
24      break;
25    } else {
26      m.set(s[i], t[i]);
27      n.set(t[i], s[i]);
28    }
29  }
30
31  return flag1;
32};
33console.log(isIsomorphic(s, t));
34console.log(isIsomorphic(s1, t1));
35console.log(isIsomorphic(s2, t2));
36console.log(isIsomorphic(s3, t3));
37