Find and Replace Pattern

MediumStringshashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A word *follows* a pattern if there is a one-to-one letter mapping (a bijection) that turns the pattern into the word when applied to every letter: equal letters in the pattern map to equal letters in the word, and different letters map to different letters. Given a list of words and a string pattern, all of the same length, return the words that follow pattern, keeping them in the same order as they appear in words.

Examples

Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
"mee" uses a->m, b->e. "ccc" fails because a and b would both map to c.
Input: words = ["a","b","c"], pattern = "a"
Output: ["a","b","c"]
Any single letter follows a one-letter pattern.

Constraints

  • `1 <= pattern.length <= 20`
  • `1 <= words.length <= 50`
  • `words[i].length == pattern.length`
  • All strings contain only lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems