Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Match words with a dot wildcard

    MediumAbout 25 minFull AccessLearn it: Tries in C#: Prefix Search, Autocomplete and Wildcard Words

    The task

    A crossword helper stores dictionary words and answers questions such as "c.t": is there a stored three-letter word that starts with c and ends with t?

    Complete the class WildcardWords. Add(word) stores a word; words are not empty and hold only the lowercase letters a to z. Matches(pattern) returns true when some stored word has the same length as pattern and matches it letter by letter, where '.' in the pattern matches any one letter. Patterns hold only lowercase letters and dots, and are not empty.

    Signature

    public void Add(string word)
    public bool Matches(string pattern)

    Examples

    • Add("cot"), Add("cut"), then Matches("c.t") and Matches("cat") → true, false. The dot matches o or u, but no stored word has an a there.
    • Add("cot"), then Matches("..") and Matches("...") → false, true. A pattern only matches words of its own length.

    Limits: Up to 100,000 words of up to 20 letters, and up to 200,000 patterns.

    The tests check: a set with no words, a dot in each position, only dots, the length, one-letter words, a dot whose first branch leads nowhere, and 30,000 words that begin the same way with 200,000 patterns, and patterns of only dots, in time.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public sealed class WildcardWords
      {
          // Stores a word of lowercase letters a to z.
          public void Add(string word)
          {
              throw new NotImplementedException();
          }
      
          // True when some stored word of the same length matches; '.' matches any one letter.
          public bool Matches(string pattern)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.