Isomorphic Strings

IF
AlgoAxiomStaff Engineers
JSTS
Easy20 mins

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

Two strings s and t are isomorphic if the characters in s can be replaced to get t, with a consistent one-to-one mapping. All occurrences of a character must be replaced with the same character while preserving the order. No two characters may map to the same character, but a character may map to itself.

Examples

Example 1:

Input: s = "egg", t = "add"

Output: true

Explanation: The mapping is: 'e' -> 'a', 'g' -> 'd'. Each character in s maps to exactly one character in t and vice versa.

Example 2:

Input: s = "foo", t = "bar"

Output: false

Explanation: 'o' would need to map to both 'a' and 'r', which violates the one-to-one constraint.

Example 3:

Input: s = "paper", t = "title"

Output: true

Explanation: The mapping is: 'p' -> 't', 'a' -> 'i', 'e' -> 'l', 'r' -> 'e'.

Constraints

  • 1 <= s.length <= 5 * 10⁴
  • t.length == s.length
  • s and t consist of any valid ASCII character
Source: Hash Maps pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle