Advertisement

Design Add and Search Words Data Structure - LeetCode 211 Solution

Design Add and Search Words Data Structure - Complete Solution Guide

Design Add and Search Words Data Structure is LeetCode problem 211, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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. Example: Input ["WordDictionary","addWord","add

Detailed Explanation

The problem requires designing a data structure, `WordDictionary`, that supports two operations: `addWord(word)` and `search(word)`. `addWord` stores a given word into the data structure. `search(word)` checks if a given word (which may contain the wildcard character '.') exists in the data structure. The '.' character can match any single character. Essentially, we need to implement a dictionary that supports regular expression-like searching where '.' acts as a single-character wildcard.

Solution Approach

The solution uses a Trie data structure to store the words added to the dictionary. The `addWord` function inserts the word into the Trie, creating new nodes for characters that don't exist. The `search` function performs a depth-first search (DFS) on the Trie. If the current character in the search word is a '.', it explores all possible children of the current Trie node. If it's a normal character, it checks if that character exists as a child and recursively continues the search. The DFS returns true if a matching path is found, and false otherwise.

Step-by-Step Algorithm

  1. Step 1: **Initialize Trie:** The `WordDictionary` is initialized with an empty Trie, typically represented as a dictionary or hash map.
  2. Step 2: **`addWord(word)`:** Iterate through the characters of the input `word`. For each character, check if it exists as a child node in the current Trie node. If not, create a new child node for that character. Move to the child node. After processing all characters, mark the last node as the end of a word by adding a special character like '$' to the node's children.
  3. Step 3: **`search(word)`:** Implement a depth-first search (DFS) function. The DFS function takes the current Trie node, the search word, and the current index in the search word as input.
  4. Step 4: **DFS Base Case:** If the index reaches the end of the search word, check if the current Trie node is marked as the end of a word (has the '$' child). If so, return true; otherwise, return false.
  5. Step 5: **DFS Wildcard Handling:** If the current character in the search word is '.', iterate through all the children of the current Trie node (excluding the end-of-word marker). Recursively call the DFS function for each child node with the index incremented.
  6. Step 6: **DFS Character Matching:** If the current character in the search word is not a '.', check if it exists as a child in the current Trie node. If not, return false. If it exists, recursively call the DFS function with the child node and the index incremented.
  7. Step 7: **DFS Return:** The `search` function initiates the DFS from the root of the Trie and returns the result of the DFS.

Key Insights

  • Insight 1: A Trie (prefix tree) is a suitable data structure for storing the words, as it allows efficient prefix-based searching.
  • Insight 2: The wildcard character '.' in the search operation necessitates a depth-first search (DFS) approach to explore all possible matching paths in the Trie.
  • Insight 3: The end-of-word marker is crucial for distinguishing between prefixes and complete words during the search.

Complexity Analysis

Time Complexity: O(m*26^n)

Space Complexity: O(m*n)

Topics

This problem involves: String, Depth-First Search, Design, Trie.

Companies

Asked at: Atlassian, Datadog, Docusign, DoorDash, LinkedIn, Rubrik, Snowflake.