Isomorphic Strings
Isomorphic Strings is LeetCode 205 (Easy). Given two strings s and t of the same length, return true if they are isomorphic.
- They are isomorphic if the characters of
scan be replaced to gett. - Every copy of a character must be replaced by the same character, and the order stays the same.
- No two different characters may turn into the same character, but a character may turn into itself.
- The strings can hold any ASCII character, not only lowercase letters.
- 1 <= s.length <= 5 * 10⁴
- t.length == s.length
- s and t consist of any valid ascii character.
Intuition
Line the strings up and read them as pairs of characters at the same index. They are isomorphic exactly when no two pairs contradict each other, in either direction:
- s to t: a character of
salways sits above the same character oft. - t to s: a character of
talways sits below the same character ofs.
So keep one map per direction and test every pair against both. The second map is the part people skip. One map only stops a character of s from having two partners; it does nothing to stop two characters of s from sharing one partner in t.
"Replace every copy of a character the same way, with no two characters sharing a replacement" describes a one-to-one mapping. Comparing two strings this way, or a pattern against a list of words, needs a map in each direction.
Approach
Before reading on, write the strings "paper" and "title" one above the other and list the pairs of characters in each column. What would a pair that breaks the rule look like? Then find a pair of strings where every character of s has one partner, yet the answer is still false.
Keep a map for each direction
s_to_t[a] holds the partner chosen for character a of s, and t_to_s[b] the partner chosen for character b of t. Both start empty, and every pair is written into both, so the two maps always mirror each other.
Test each pair against both maps
Walk the strings together. For the pair a, b, s_to_t.get(a, b) gives back b when a has no partner yet, so the test fails only when a already has a different partner. The same test on t_to_s fails when b is already taken by another character. Either failure returns false.
Record the pair
If both tests pass, store a → b and b → a. For a pair seen before, this writes the same values again, which is harmless and saves a separate branch.
Return true after the last index
Reaching the end means every index agreed with both maps. The pairs then rename s into t one character at a time, with each character used by exactly one partner.
Code in Python, C++ and Java
Common pitfalls
Checking only the s to t direction
for a, b in zip(s, t):
if s_to_t.get(a, b) != b:
return False
s_to_t[a] = bfor a, b in zip(s, t):
if s_to_t.get(a, b) != b or t_to_s.get(b, a) != a:
return False
s_to_t[a] = b
t_to_s[b] = aFor s = "badc", t = "baba" every character of s is new when it first appears, so this returns true. But b and d would both turn into b, which the rules forbid; the answer is false.
Comparing only the number of distinct characters
return len(set(s)) == len(set(t))
return len(set(zip(s, t))) == len(set(s)) == len(set(t))
"aab" and "abb" both have two distinct characters, but the first a pairs with a and the second with b. The counts say nothing about where each character sits.
Edge cases
A dictionary accepts any character. If you swap the maps for arrays, size them for all 128 ASCII codes; an array of 26 indexed by ord(c) - ord('a') breaks on " ".
Complexity
Isomorphic Strings FAQ
Is there a one-line check?
Yes: len(set(zip(s, t))) == len(set(s)) == len(set(t)). If there are as many distinct pairs as distinct characters of s, no character of s appears in two different pairs. If there are also as many as distinct characters of t, no character of t does either. Together that is the one-to-one rule.
If s is isomorphic to t, is t isomorphic to s?
Always. A one-to-one renaming of s into t can be read backwards to rename t into s. That is a quick way to see why one map is not enough: a one-way check would give different answers for (s, t) and (t, s).
How is this different from Word Pattern?
Word Pattern (LeetCode 290) asks the same question with a pattern of letters on one side and the words of a sentence on the other. Split the sentence into words, check that there are as many words as letters, then use the same two maps.