77. Word Pattern
mediumAsked at SalesforceDetermine if a string follows the same pattern as a sequence of words. Salesforce uses this to test bidirectional hashmap invariants in a more complex setting.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses bijective mapping in their field-mapping wizard.
- LeetCode Discuss (2025)— Pairs with LC 205 (Isomorphic Strings).
Problem
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.
Constraints
1 <= pattern.length <= 300pattern contains only lower-case English letters.1 <= s.length <= 3000s contains only lowercase English letters and spaces ' '.s does not contain any leading or trailing spaces.All the words in s are separated by a single space.
Examples
Example 1
pattern = "abba", s = "dog cat cat dog"trueExample 2
pattern = "abba", s = "dog cat cat fish"falseExample 3
pattern = "aaaa", s = "dog cat cat dog"falseApproaches
1. Single-direction map (broken)
Map pattern[i] to words[i]; check consistency.
- Time
- O(n)
- Space
- O(n)
// Fails on 'abba', 'dog dog dog dog' — pattern 'a' maps to 'dog' consistently; 'b' maps to 'dog' too, but a and b should map to different words.Tradeoff: Same trap as LC 205.
2. Bidirectional map
Maintain pattern->word and word->pattern maps; both must be consistent.
- Time
- O(n)
- Space
- O(n)
function wordPattern(pattern, s) {
const words = s.split(' ');
if (pattern.length !== words.length) return false;
const pToW = new Map(), wToP = new Map();
for (let i = 0; i < pattern.length; i++) {
const p = pattern[i], w = words[i];
if (pToW.has(p) && pToW.get(p) !== w) return false;
if (wToP.has(w) && wToP.get(w) !== p) return false;
pToW.set(p, w);
wToP.set(w, p);
}
return true;
}Tradeoff: Bijective check via two maps. The length check upfront prevents off-by-one when pattern and words don't align.
Salesforce-specific tips
Salesforce uses this exact bijective mapping in their field-mapping wizard (each source field must map to exactly one target field and vice versa). They grade specifically on detecting the 'aaaa' → 'dog cat cat dog' case where forward map is fine but reverse fails. Bonus signal: discuss Word Pattern II (LC 291) which extends with backtracking.
Common mistakes
- Forgetting to check pattern.length === words.length — gives false positives on length mismatch.
- Single-direction map — same trap as LC 205.
- Using object keys for words — works in JS but breaks if any word collides with Object prototype (e.g., 'toString').
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Word Pattern II (LC 291) — pattern matches without space delimiters.
- Isomorphic Strings (LC 205).
- Generalize to multi-character patterns.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why two maps?
Because bijectivity is two-way. Forward map enforces 'pattern char → word' consistency. Reverse map enforces 'word → pattern char' consistency.
Why split on space?
The problem says words are space-separated. split(' ') handles single-space delimiters correctly; for multi-space, use split(/\s+/).
Practice these live with InterviewChamp.AI
Drill Word Pattern and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →