The Isomorphic Strings interview question asks you to determine if two strings, s and t, are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. The rules are:
This is a popular question at companies like Apple, Amazon, and Uber. It tests your proficiency with Hash Table interview patterns and your ability to maintain a one-to-one mapping (bijective mapping). It evaluations whether you can identify that a single map is often not enough to ensure that two different characters in s don't map to the same character in t.
This problem follows the Character Mapping (Dual-Map or Position tracking) pattern.
s -> t and one for t -> s.lastSeenS[c1] != lastSeenT[c2], they aren't mapping consistently.s = "egg", t = "add"
mapST={e:a}, mapTS={a:e}.mapST={e:a, g:d}, mapTS={a:e, d:g}.t[2]. OK.
Result: True.
s = "foo", t = "bar"t[2] is 'r'. Conflict!
Result: False.s -> t), which fails when two different characters in s map to the same character in t (e.g., s="ab", t="aa").Always remember that a one-to-one mapping requires bi-directionality. In any mapping problem, ask yourself: "Can two inputs map to the same output?" If the answer is no, you need to check both directions.