#205

Isomorphic Strings

Easy Two-way mapping

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether characters of s can be replaced one-to-one to get t (no two characters may map to the same character).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can letters of s be renamed one-to-one to get t?

Why this works: check the mapping in both directions; "ab" → "aa" fails because two letters would map to the same one.

Example: "egg" → "add"

e→a, g→d, g→d ✓ and back a→e, d→g ✓ → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Checking only one direction.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(charset).
Topic: Strings
🎬 Episodes 16–20: video coming soon