Skip to the code
cf.
+25 XP

#370 Wildcard Command Search

medium

A command palette lets power users type patterns where . stands for any single letter, so s.ve matches save. You need a store of command names that answers whether a pattern matches some whole command.

Write a class CommandIndex with:

  • constructor(): starts empty.
  • add(name): stores the command name (lowercase letters). Returns nothing.
  • matches(pattern): returns true if some stored name has the same length as pattern and agrees with it at every position, where . agrees with any letter.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new CommandIndex(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["CommandIndex","add","add","matches","matches","matches"]
[[],["save"],["open"],["s.ve"],["o..n"],["sav"]]
Output: [null,null,null,true,true,false]

s.ve matches save and o..n matches open; sav is too short to match a whole name.

Example 2

Input: ["CommandIndex","add","matches","matches"]
[[],["copy"],["...."],["....."]]
Output: [null,null,true,false]

Four dots match any four-letter name.

Constraints

  • 1 <= name.length, pattern.length <= 25
  • Patterns have at most 3 dots.
  • At most 10^4 calls.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).