Match a Pattern to a String Under a Letter-to-Substring Bijection

Last updated: November 29, 2026

Quick Overview

Given a pattern of letters and a string, decide whether there is a one-to-one mapping from pattern letters to non-empty substrings such that replacing each letter with its substring produces the string exactly.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a pattern of letters and a string, decide whether there is a one-to-one mapping from pattern letters to non-empty substrings such that replacing each letter with its substring produces the string exactly.

A Google onsite backtracking problem with a two-way mapping invariant. The interviewer checks that you enforce injectivity in both directions, reuse an existing mapping without branching, and prune by remaining length.

What the Interviewer Expects
  • Maintains letter-to-substring and substring-to-letter maps
  • For an already mapped letter, checks the prefix and moves on without branching
  • For a new letter, tries every non-empty substring not already claimed
  • Undoes both maps on backtrack
  • Prunes using the minimum length the rest of the pattern needs
Key Topics to Cover
Backtracking
Bijection
Hash maps
String matching
Pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you prune using the known lengths of already-mapped letters?
  • What if the mapping does not need to be one-to-one?
  • How would you return the mapping itself?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: pattern (lowercase letters, up to about 20) and s (up to about 20 to 30 characters).
  • Output: True if a bijection exists.
  • Example: "abab", "redblueredblue" is True (a = red, b = bl...
High-Level Design

Recurse on (i, j): pattern index and string index.

  • If both are at the end, success. If only one is, fail.
  • Let p = pattern[i]. If p is already mapped to w, the string must continue with w: che...

Submit Your Answer
Markdown supported