Isomorphic strings
Two strings s and t have the same length. They are isomorphic (they have the same shape) when replacing the characters of s can turn it into t under two rules:
- Every copy of a character gets the same replacement. If one
abecomesx, everyabecomesx. - Different characters get different replacements. If
abecomesx, no other character can also becomex.
All replacements happen at once, and a character may become itself: "abc" and "abd" are isomorphic, since a and b stay and c becomes d. Return True if s and t are isomorphic, and False otherwise.
s = "ball", t = "tree"OutputTrueb becomes t, a becomes r, and l becomes e. Both copies of l become e.
s = "moon", t = "noon"OutputFalsem would become n, and n would also stay n. Two different characters cannot become the same one.
s = "aab", t = "xyz"OutputFalseThe first a becomes x, so the second a must become x too, but it lines up with y.
1 ≤ len(s) ≤ 5 × 104
len(t) == len(s)
Characters can be any ASCII characters, including digits, spaces and punctuation.
Plan it first
Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.