AlgoFrames

Word dictionary with wildcard

Challenge · Write code · about 20 min

Build a word dictionary and run a list of commands. add W stores the word W. search P is true when some stored word matches the pattern P. In P, a dot . matches any single letter, and every other letter must match itself. The word must have the same length as P.

Return the results of the search commands in order, as the text true or false.

Example 1

Input:

[["add bad","add dad","add mad","search pad","search .ad","search b.."]]

Output:

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

A dot tries every child of the node.

Example 2

Input:

[["add a","search .","search .."]]

Output:

["true","false"]

The pattern length must equal the word length.

Constraints

  • 0 ≤ commands.length ≤ 2,000
  • Each command is add W or search P
  • W has 1 to 8 lowercase letters. P has 1 to 8 characters: lowercase letters or dots

Learn the idea first: Tries: insert, search, prefix, a visual lesson.

More Tries: insert, search, prefix problems