Isomorphic Strings

Medium~15 min

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t, with a one-to-one mapping between characters. Every occurrence of a character in s must map to the same character in t, and no two different characters in s may map to the same character in t.

The mapping must work in both directions: each character in s maps to exactly one character in t, and each character in t is mapped to by exactly one character in s.

Examples

Example 1
Input: s = "egg", t = "add"
Output: true
Explanation: e -> a, g -> d. Each character maps to exactly one other character, and no two characters map to the same target.
Example 2
Input: s = "foo", t = "bar"
Output: false
Explanation: o would need to map to both "a" and "r" (first o -> a, second o -> r), which violates the one-to-one mapping.
Example 3
Input: s = "paper", t = "title"
Output: true
Explanation: p -> t, a -> i, e -> l, r -> e. Each mapping is consistent and unique.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • s.length == t.length
  • s and t consist of printable ASCII characters
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run