Isomorphic Mapping

Medium~15 min

Given two strings s and t of the same length, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t, with a consistent one-to-one mapping. 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. A character may map to itself.

Return true if the strings are isomorphic, false otherwise.

Examples

Example 1
Input: s = "egg", t = "add"
Output: true
Explanation: e maps to a, g maps to d. Each mapping is consistent and one-to-one.
Example 2
Input: s = "foo", t = "bar"
Output: false
Explanation: o would need to map to both a and r, which violates the consistent mapping rule.
Example 3
Input: s = "paper", t = "title"
Output: true
Explanation: p->t, a->i, e->l, r->e. All mappings are consistent and one-to-one.
Example 4
Input: s = "badc", t = "baba"
Output: false
Explanation: b->b and d->b means two different characters map to the same character.

Constraints

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

Run your code to see results

Use Cmd+Enter to run