Asked at 8 companies
1 recent
- Dropbox
- Uber
- Nvidia
- Bloomberg
- Meta
- Microsoft
- Amazon
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a pattern and a string s, find if s follows the same pattern.ReadHide
Problem Statement
Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s. Specifically:
- Each letter in
patternmaps to exactly one unique word ins. - Each unique word in
smaps to exactly one letter inpattern. - No two letters map to the same word, and no two words map to the same letter.
Added 2026-11-25 from Srujan's LeetCode Recent AC list (photo) — not previously in the repo.
Input / Output
Input: pattern = "abba", s = "dog cat cat dog"
Output: true
Explanation:
The bijection can be established as:
- 'a' maps to "dog".
- 'b' maps to "cat".
Input: pattern = "abba", s = "dog cat cat fish"
Output: false
Input: pattern = "aaaa", s = "dog cat cat dog"
Output: false
Code it on LeetCode
There's no editor for this one here yet, so solve it on LeetCode, with its own tests. Then tick My code passed below, and it joins your reviews. With your LeetCode account linked, that happens on its own.
An error or a Wrong Answer there? What each one means.
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code
Approach
Pattern Recognition
Bijection needs two maps — one direction is not enough. Checking only "each letter maps to one word" would let a and b both map to "dog", which is not a bijection. Keep a letter→word map and a word→letter map, and fail the moment either one disagrees.
Approach
Brute Force — Compare Every Pair, O(n^2)
words = s.split()
if len(pattern) != len(words): return False
for i in range(n):
for j in range(n):
if (pattern[i] == pattern[j]) != (words[i] == words[j]):
return False
return True
Two positions must agree on both sides: same letter exactly when same word. Correct, but every pair gets checked.
Optimized — Two Hash Maps, O(n)
words = s.split()
if len(pattern) != len(words): return False
p2w, w2p = {}, {}
for ch, w in zip(pattern, words):
if ch in p2w and p2w[ch] != w: return False
if w in w2p and w2p[w] != ch: return False
p2w[ch] = w
w2p[w] = ch
return True
Example: "abba" / "dog cat cat dog" — a→dog, b→cat, second b meets cat again, last a meets dog again → true. But "aaaa" / "dog cat cat dog" fails at the second word: a already maps to dog, not cat.
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Compare all pairs | O(n^2) | O(1) | Every position checked against every other |
| Two hash maps | O(n) | O(n) | One pass; both directions enforced |
Solution code
class SolutionBruteForce:
"""O(n^2) time | O(1) space — same letter exactly when same word, for every pair."""
def wordPattern(self, pattern: str, s: str) -> bool:
words = s.split()
if len(pattern) != len(words):
return False
n = len(pattern)
for i in range(n):
for j in range(n):
if (pattern[i] == pattern[j]) != (words[i] == words[j]):
return False
return True
class Solution:
"""O(n) time | O(n) space — two maps enforce the bijection in one pass."""
def wordPattern(self, pattern: str, s: str) -> bool:
words = s.split()
if len(pattern) != len(words):
return False
p2w, w2p = {}, {}
for ch, w in zip(pattern, words):
if ch in p2w and p2w[ch] != w:
return False
if w in w2p and w2p[w] != ch:
return False
p2w[ch] = w
w2p[w] = ch
return TrueOther ways to solve it
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language