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, where each character maps to exactly one other character and preserving order. No two characters may map to the same character.
s = "egg", t = "add"trues = "foo", t = "bar"falsesToT = {} and tToS = {} (bijection maps)i: get sc = s[i], tc = t[i]sToT[sc] exists and sToT[sc] ≠ tc → return falsetToS[tc] exists and tToS[tc] ≠ sc → return falsesToT[sc] = tc and tToS[tc] = scIsomorphic = one-to-one character mapping (bijection). A single map is insufficient — e.g. "ab"→"aa" would pass a one-direction check since both a and b map to a. Two maps enforce both directions: every s-char maps to exactly one t-char and every t-char maps back to exactly one s-char.
Checking only one direction is the common mistake. For badc and baba, b maps to b and a to a, but then d would also map to b, which must not happen.
Keep two maps: one from the first string to the second, and one from the second back to the first. At each position, the pair must agree with both maps, or be new to both.
Isomorphic means a one-to-one mapping, so check both directions. Two maps, one for each direction.
Run the animation above on your own input, or watch the 1:24 video explanation of Isomorphic Strings with a dry run and Java code.