codejump
EasyPythonJavaScriptTypeScript

Strings — Isomorphic Strings

AlgorithmsStringsHash Map

solve(s, t) — two strings of the same length are isomorphic if the characters of s
can be replaced to get t, with a one-to-one mapping: every character of s always maps
to the same character of t, and two different characters of s never map to the same
character.

One map is not enough: "ab" → "aa" is a valid function but not a valid *bijection*. Keep
two maps (s → t and t → s), or compare the index of each character's first occurrence.

Examples

solve("egg", "add")      → true
solve("foo", "bar")      → false
solve("badc", "baba")    → false      // d and b would both map to a

Sample tests

Test #1Valid mapping
Input: ["egg","add"]
Output: true
Test #2o maps to two letters
Input: ["foo","bar"]
Output: false
Test #3Longer valid mapping
Input: ["paper","title"]
Output: true