KatInterview

Wildcard Word Dictionary Simulator

Data Structure DesignAuto-graded · 26 test cases
MediumAuto-gradedData Structure Designtriedesignwildcard

Last updated

Simulate a word store that answers pattern queries where '.' stands for any single letter, which is best handled with a prefix tree and a depth-aware traversal.

Wildcard Word Dictionary

You are building a small word store that supports two commands:

  • addWord: save a word of lowercase English letters.
  • search: given a pattern, report whether at least one saved word matches it.

A pattern is made of lowercase letters and the wildcard .. A pattern matches a word when both have the same length and, at every position, either the pattern character is . (which matches any single lowercase letter) or the two characters are identical.

Rather than a class, you write one function that replays a whole script of commands and returns the outputs.

Input

  • operations: a list of command names. Each is either "addWord" or "search".
  • arguments: a list of the same length. arguments[i] is a list holding exactly one string, which is the word (for addWord) or the pattern (for search) belonging to operations[i].

Output

Return a list with one entry per command, in order:

  • None for each addWord command.
  • True or False for each search command, telling whether some word added earlier in the script matches the pattern.

If a command name is neither "addWord" nor "search", raise ValueError.

Rules and edge cases

  • Only words added before a search are considered.
  • Adding the same word more than once has no additional effect.
  • A saved word that is merely a prefix of the pattern, or that the pattern is a prefix of, does not match, because the lengths must be equal.
  • The empty string is a valid word and a valid pattern. The pattern "" matches only if the empty word was added.
  • A pattern made entirely of dots matches any saved word of that exact length.
  • A search before any word has been added returns False.

Constraints

  • Number of commands: up to about 10^5.
  • Length of each word or pattern: up to about 10^3.
  • Words contain only a-z. Patterns contain only a-z and ..
  • A solution that compares the pattern against every stored word may be too slow. Aim to share work between words with common prefixes.

Examples

Example 1

Input: run_wildcard_dictionary(['addWord', 'addWord', 'addWord', 'search', 'search', 'search', 'search'], [['bad'], ['dad'], ['mad'], ['pad'], ['bad'], ['.ad'], ['b..']])

Output: [None, None, None, False, True, True, True]

Example 2

Input: run_wildcard_dictionary(['addWord', 'addWord', 'search', 'search', 'search', 'search'], [['a'], ['ab'], ['.'], ['a'], ['a.'], ['..']])

Output: [None, None, True, True, True, True]

Hints

Hint 1

Words that start the same way can share storage. Think about a prefix tree where each node knows whether a stored word ends there.

Hint 2

A normal letter in the pattern follows exactly one branch. A '.' needs to try every branch that exists at the current node.

Hint 3

Success needs two things: the whole pattern has been consumed, and the node you are standing on marks the end of a stored word. Merely existing as a path is not enough. Use an explicit stack if you want to avoid deep recursion.