# The Letter Mapper

> A consistent substitution, or not.

Canonical URL: <https://datadriven.io/problems/the_letter_mapper>

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given two equal-length strings s and t, return True if they are isomorphic: there exists a bijection between characters of s and t such that each s[i] maps to t[i] consistently (no two different characters map to the same character).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **bijective mapping verification**, a pattern used in encoding validation and schema mapping. It probes whether a candidate can enforce both forward and reverse mapping uniqueness simultaneously.

---

### Break down the requirements

#### Step 1: Build forward and reverse mappings

Map each character in string A to the corresponding character in string B, and vice versa.

#### Step 2: Check consistency on every pair

If a character in A was previously mapped to a different character in B (or vice versa), the strings are not isomorphic.

---

### The solution

**Dual-mapping consistency check**

```python
def is_isomorphic(s: str, t: str) -> bool:
    map_s_to_t = {}
    map_t_to_s = {}
    for i in range(len(s)):
        ch_s = s[i]
        ch_t = t[i]
        if ch_s in map_s_to_t:
            if map_s_to_t[ch_s] != ch_t:
                return False
        else:
            map_s_to_t[ch_s] = ch_t
        if ch_t in map_t_to_s:
            if map_t_to_s[ch_t] != ch_s:
                return False
        else:
            map_t_to_s[ch_t] = ch_s
    return True
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the string length.
> 
> **Space:** O(k) where k is the number of unique characters across both strings.

> **Interviewers Watch For**
>
> Whether you check both directions. Only checking forward (s to t) misses cases where two different characters in s map to the same character in t.

> **Common Pitfall**
>
> Only building the forward mapping. `'ab'` and `'aa'` would pass a forward-only check since `a->a` and `b->a` are consistent forward, but `a` in t maps to both `a` and `b` in s.

---

## Common follow-up questions

- What if the strings have different lengths? _(Tests immediate return False since bijection requires equal length.)_
- How is this different from checking anagrams? _(Tests understanding that isomorphism checks structure (mapping), not content (same characters).)_
- How would you find the actual mapping if it exists? _(Tests returning the forward map dict.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_letter_mapper)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.