25 · Tries

#211 Design Add and Search Words Data Structure

Mediumsolve on LeetCode ↗

frequently reported atByteDance / TikTokAmazonAppleMicrosoftMetaGoogle

the problem

In plain words

Design WordDictionary with addWord(word) and search(pattern), where the pattern may contain . meaning "any one letter". Return whether some added word matches.

in addWord("bad"), addWord("dad"), addWord("mad"), search("pad"), search("bad"), search(".ad"), search("b..")

out null, null, null, false, true, true, true

  • 1 ≤ word.length ≤ 25
  • addWord: lowercase letters; search: lowercase letters or '.'
  • At most 2 dots per search query
  • At most 10⁴ calls

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #208
    2. #212
      Word Search II
      Hardin Tries
    3. #79
      Word Search
      Mediumin Backtracking