Tutorialspoint
Problem
Solution
Submissions

Adding and Searching Words

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Design a data structure that supports adding new words and finding if a string matches any previously added string. The string may contain dots '.' where each dot can match any letter.

Example 1
  • Input: ["WordDictionary", "addWord", "addWord", "addWord", "search", "search", "search", "search"]
    [[], ["bad"], ["dad"], ["mad"], ["pad"], ["bad"], [".ad"], ["b.."]]
  • Output: [null, null, null, null, false, true, true, true]
  • Explanation:
    • WordDictionary wordDictionary = new WordDictionary();
    • wordDictionary.addWord("bad");
    • wordDictionary.addWord("dad");
    • wordDictionary.addWord("mad");
    • wordDictionary.search("pad"); // return false
    • wordDictionary.search("bad"); // return true
    • wordDictionary.search(".ad"); // return true, '.' matches any letter
    • wordDictionary.search("b.."); // return true, '.' matches any letter
Example 2
  • Input: ["WordDictionary", "addWord", "addWord", "search", "search", "search", "search"]
    [[], ["apple"], ["banana"], ["a...."], ["apple"], ["ban"], ["b....a"]]
  • Output: [null, null, null, true, true, false, true]
  • Explanation:
    • WordDictionary wordDictionary = new WordDictionary();
    • wordDictionary.addWord("apple");
    • wordDictionary.addWord("banana");
    • wordDictionary.search("a...."); // return true, matches "apple"
    • wordDictionary.search("apple"); // return true
    • wordDictionary.search("ban"); // return false
    • wordDictionary.search("b....a"); // return true, matches "banana"
Constraints
  • 1 ≤ word.length ≤ 25
  • word consists of lowercase English letters and possibly dots '.'
  • There will be at most 3 dots in word for search queries
  • At most 10^4 calls will be made to AddWord and Search
  • Time Complexity: O(N) for AddWord, where N is the length of the word
  • Space Complexity: O(N * M) where N is the total number of characters in all words
Trie KPMGTutorix
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Consider using a Trie (Prefix Tree) data structure to efficiently store and search for words
  • Each node in the Trie represents a character in a word
  • For the Search operation with dots, use a recursive approach to try all possible characters at positions where dots appear
  • Mark the end of words in the Trie to differentiate between prefixes and complete words
  • The Trie approach is especially efficient for dictionary operations

Steps to solve by this approach:

 Step 1: Design a Trie data structure with nodes that contain a dictionary of children (mapping characters to nodes) and a boolean flag indicating if the node represents the end of a word.
 Step 2: For AddWord, iterate through each character of the word, creating new nodes as needed, and mark the last node as the end of a word.
 Step 3: For Search, use a recursive approach to handle the wildcard character '.'.
 Step 4: If the current character is a '.', try all possible branches in the Trie.
 Step 5: If the current character is a regular letter, follow the corresponding branch if it exists.
 Step 6: When reaching the end of the word, return true if the current node is marked as the end of a word.
 Step 7: This approach efficiently handles both exact matches and pattern-based searches with wildcards.

Submitted Code :