Skip to content
MediumTrieAI interview only

Count Wildcard Pattern Matches

Asked atgoogleamazonmetamicrosoftbloomberg

01 · Problem

You are given a dictionary words of distinct lowercase words and an array patterns. Each pattern consists of lowercase letters and the wildcard ., where . stands for exactly one arbitrary letter. A word matches a pattern when both have the same length and every non-wildcard character of the pattern equals the word's character in that position.

Return an array answer where answer[i] is the number of dictionary words that match patterns[i].

02 · Examples

Example 01
Input
words = ["bad","dad","mad","pad","bat"], patterns = ["b..",".ad","...."]
Output
[2,4,0]

"b.." matches "bad" and "bat". ".ad" matches "bad", "dad", "mad" and "pad". No word has length 4.

Example 02
Input
words = ["a","b","ab"], patterns = [".","..","a","c"]
Output
[2,1,1,0]

"." matches both one-letter words, ".." matches "ab", "a" matches itself, and "c" matches nothing.

03 · Constraints

  • 011 <= words.length <= 2000
  • 021 <= patterns.length <= 2000
  • 031 <= words[i].length, patterns[j].length <= 15
  • 04words[i] consists of lowercase English letters; all words are distinct
  • 05patterns[j] consists of lowercase English letters and '.'

04 · Optimal complexity

Time
O(W + p * N)
Space
O(W)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.