hardArrays & HashingAI-applied~30 min
Which Token Strings Match a Structural Pattern
A pattern describes a token-shape via placeholder letters; a token string matches if there is a one-to-one renaming of letters to characters that reproduces it. Return every token string that matches.
Problem
Given a list of strings words and a string pattern of the same length, return the words that match the pattern. A word matches if there is a bijection between letters in pattern and characters in the word that maps pattern to the word.
Input
A list words and a string pattern.
Output
A list of the matching words.
Constraints
- 1 <= words.length <= 50, 1 <= pattern.length <= 20
- All words have the same length as pattern.
- Words and pattern are lowercase letters.
Examples
Example 1
Input
words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output
["mee","aqq"]
mee and aqq both follow the a-b-b shape with a bijection.
Example 2
Input
words = ["a","b","c"], pattern = "a"
Output
["a","b","c"]
Any single character matches a length-1 pattern.