#290

Word Pattern

Easy Two-way HashMap

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

?The problem

Decide whether a sentence follows a pattern of letters, where each letter maps to exactly one word and each word to exactly one letter.

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

◎Understanding

Really asking: does each letter stand for exactly one word, and each word for one letter?

Why this works: two maps, one per direction; any conflict fails.

Example: "abba" and "dog cat cat dog"

a→dog, b→cat, b→cat ✓, a→dog ✓ → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Checking only char → word, so "abba" with "dog dog dog dog" passes wrongly.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(n).
Topic: Hashing
🎬 Episodes 37–39: video coming soon