Skip to content

Isomorphic Strings

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

Two strings s and t are isomorphic if you can replace characters in s to get t, where every occurrence of a character is replaced by the same character, and no two different characters map to the same character. A character may map to itself. Return true if s and t are isomorphic.

s = "egg",   t = "add"     →  true   (e→a, g→d)
s = "foo",   t = "bar"     →  false  (o would need to become both a and r)
s = "badc",  t = "baba"    →  false  (b and d would both become b)

Constraints

  • 1 <= s.length <= 5 * 10^4
  • t.length == s.length
  • s and t consist of any valid ASCII characters.