Menu
CoddyTech

Design Add and Search Words Data Structure

Build a word dictionary with two operations. addWord w stores the word w. search p tells whether some stored word matches the pattern p: same length, and at every position the pattern holds the same letter or a dot ., which stands for any one letter.

Operation i is ops[i] on the word or pattern words[i]. Run them in order on an empty dictionary and return one string each: "null" for an addWord, "true" or "false" for a search.

Function

wordDictionaryOps(ops: string-array, words: string-array) → string-array
opsstring-array
the operations, in the order they run
wordsstring-array
the word to add or the pattern to search for, one per operation
Returnsstring-array
one answer per operation, as text

Constraints

  • 1 ≤ ops.length ≤ 2500
  • words.length == ops.length
  • Every ops[i] is addWord or search.
  • 1 ≤ words[i].length ≤ 20
  • A word to add holds only lowercase English letters.
  • A pattern to search holds lowercase English letters and at most 3 dots.

Examples

Input
ops = ["addWord", "addWord", "search", "search", "search", "search"]words = ["rose", "rise", "r.se", "ros", "rose.", "..s."]
Output
["null", "null", "true", "false", "false", "true"]
Explanation
r.se matches both words: the dot covers the o of rose and the i of rise. A dot never stands for a missing letter, so ros and rose. fail. ..s. matches, since both words have s third.
challenge icon

Follow-up

How would you extend search if a pattern could also hold *, standing for any run of letters, including an empty one?

Reset code
def wordDictionaryOps(ops, words):
    # Write code here

Test #1

Test #2

Test #3

Arguments

ops = ["addWord", "addWord", "search", "search", "search", "search"]
words = ["rose", "rise", "r.se", "ros", "rose.", "..s."]

Expected Output

["null", "null", "true", "false", "false", "true"]