Explorer
Data Structures & Algorithms

Determine if two strings are isomorphic using character mapping.

Problem Statement

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`. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

Examples

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

Output: true

Explanation: Each character maps uniquely to a target character preserving order.

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

Output: false

Explanation: Characters cannot be mapped consistently between both strings.

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

Output: true

Explanation: Each character maps uniquely to a target character preserving order.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Isomorphic requires a bijection (one-to-one and onto mapping) between characters of s and t. šŸ’” Hint 2: No two characters may map to the same character, and a character cannot map to multiple characters. šŸ’” Hint 3: Maintain two maps: mapS for s[i] -> t[i] and mapT for t[i] -> s[i].

Editorial & Approach

Problem Overview & Intuition

Two strings are isomorphic if there is a 1-to-1 bijection between characters. We can track character mappings in both directions (s -> t and t -> s) to verify that neither character maps to multiple values.

Step-by-Step Approach

  1. If s.length !== t.length, return false.
  2. Maintain two maps: m1 (s -> t) and m2 (t -> s).
  3. Iterate through index i:
  4. If m1.has(s[i]) && m1.get(s[i]) !== t[i], return false.
  5. If m2.has(t[i]) && m2.get(t[i]) !== s[i], return false.
  6. Record mappings m1.set(s[i], t[i]) and m2.set(t[i], s[i]).
  7. Return true.

Optimal Implementation (JavaScript)

function isIsomorphic(s, t) {
  if (s.length !== t.length) return false;
  const m1 = new Map(), m2 = new Map();
  for (let i = 0; i < s.length; i++) {
    const c1 = s[i], c2 = t[i];
    if (m1.has(c1) && m1.get(c1) !== c2) return false;
    if (m2.has(c2) && m2.get(c2) !== c1) return false;
    m1.set(c1, c2);
    m2.set(c2, c1);
  }
  return true;
}

Complexity Analysis

Time Complexity O(N) — single pass through strings.
Space Complexity O(1) — alphabet size bounded by 256 characters.

Edge Cases & Corner Traps Handled

  • Duplicate character mapping: "badc" and "baba" -> false.
  • Different length strings -> false.
  • Single character strings -> true.

Isomorphic String

Easy
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`. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example Scenarios
1Example 1
Input: s = "egg", t = "add"
Output: true
Explanation:

Each character maps uniquely to a target character preserving order.

2Example 2
Input: s = "foo", t = "bar"
Output: false
Explanation:

Characters cannot be mapped consistently between both strings.

3Example 3
Input: s = "paper", t = "title"
Output: true
Explanation:

Each character maps uniquely to a target character preserving order.

Editor
Loading Editor...
s =
"egg"
t =
"add"
Output:Click "Run" above to execute and verify your code here.
true