Isomorphic strings
Walk both strings together, maintaining two maps: a → b and b → a. Every pair must agree with both. One map alone allows two characters to collapse onto one, so it wrongly accepts "badc" and "baba" — which is exactly the case interviewers test.
Overview
What isomorphic means here
There must be a one-to-one correspondence between the characters: replacing every character of the first string according to a fixed rule produces the second, and no two characters map to the same target. "Same shape, different letters".
A length check is a free first rejection, and the empty string is isomorphic to itself.
Step through it
What to watch
- Each step checks the pair against both maps.
- A character already mapped elsewhere fails immediately.
- The two maps at the end are inverses of each other.
Say this out loud
"Two dictionaries, one each way, because the mapping has to be a bijection. With only the forward map you'd accept two letters mapping onto the same one."