Design Add and Search Words Data Structure - Problem

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

  • WordDictionary() - Initializes the object
  • void addWord(word) - Adds word to the data structure, it can be matched later
  • bool search(word) - Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter

Input & Output

Example 1 — Basic Operations
$ Input: operations = ["WordDictionary","addWord","addWord","addWord","search","search","search","search"], parameters = [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
Output: [null,null,null,null,false,true,true,true]
💡 Note: Initialize dictionary, add 'bad', 'dad', 'mad'. Search 'pad' (false), 'bad' (true), '.ad' matches 'bad', 'dad', 'mad' (true), 'b..' matches 'bad' (true)
Example 2 — Wildcard Pattern
$ Input: operations = ["WordDictionary","addWord","search","search"], parameters = [[],["a"],["."], ["a"]]
Output: [null,null,true,true]
💡 Note: Add 'a', search '.' matches any single character including 'a' (true), search 'a' finds exact match (true)
Example 3 — No Match
$ Input: operations = ["WordDictionary","addWord","search"], parameters = [[],["abc"],["ab"]]
Output: [null,null,false]
💡 Note: Add 'abc', search 'ab' has different length than stored word, no match (false)

Constraints

  • 1 ≤ word.length ≤ 25
  • word in addWord consists of lowercase English letters
  • word in search consist of '.' or lowercase English letters
  • At most 104 calls will be made to addWord and search

Visualization

Tap to expand
INPUTALGORITHMRESULTOperations:addWord("bad"), addWord("dad")addWord("mad")search("pad") → falsesearch("bad") → truesearch(".ad") → truePattern ".ad" means:• '.' matches any single letter• 'a' must match exactly 'a'• 'd' must match exactly 'd'Matches: bad, dad, mad1. Build TrieInsert words into tree structure2. DFS SearchTraverse paths for pattern matching3. Handle WildcardsExplore all branches when '.' found4. Check Word EndVerify complete word at leaf nodesrootbdmSearch Results:Operations Results:• WordDictionary() → null• addWord("bad") → null• addWord("dad") → null• addWord("mad") → null• search("pad") → false• search("bad") → true• search(".ad") → true• search("b..") → trueFinal Output Array:[null,null,null,null,false,true,true,true]Key Insight:Trie structure enables efficient prefix operations while recursive DFS handleswildcard patterns by exploring all possible character matches at each '.' positionTutorialsPoint - Design Add and Search Words Data Structure | Trie with DFS
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28
125.0K Views
High Frequency
~25 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen