Design Add and Search Words Data Structure

Design Add and Search Words Data Structure

Design a data structure to store words and efficiently support word addition and searching, including flexible matching.

Implement the WordDictionary class with the following methods:

  • addWord(word): Adds the given word to the data structure, allowing it to be stored for future searches. This is useful for building a collection of words dynamically.
  • search(word): Searches for a word in the data structure. The search supports both exact matches and partial matches using the . character as a wildcard. The . can represent any letter, making the search versatile for patterns or incomplete words.

This structure is particularly useful for applications like pattern matching or word suggestions.

Add and Search Words Data Structure

Explanation:

  • WordDictionary wordDictionary = new WordDictionary();
  • wordDictionary.addWord(“day”);
  • wordDictionary.addWord(“bay”);
  • wordDictionary.addWord(“may”);
  • wordDictionary.search(“say”); // return false
  • wordDictionary.search(“day”); // return true
  • wordDictionary.search(“.ay”); // return true
  • wordDictionary.search(“b..”); // return true

Constraints:

  • 1 <= word.length <= 20
  • word in addWord consists of lowercase English letters.
  • word in search consist of ‘.’ or lowercase English letters.

Design Add and Search Words Data Structure Solution

Recommendation for Time and Space Complexity – Aim for O(n) time per function call and O(t + n) space, where n is the word length and t is the number of nodes in the Trie.

Hints for solving problems

Hint 1 :

A brute force approach stores words in a list and searches through it, taking O(m * n) time. Consider a more efficient solution, like using a tree-like structure.

Hint 2 :

Use a Trie for efficient word insertion and search. For . (wildcard), recursively check all possible characters at that position.

Hint 3 :

While traversing the word:

  • Match characters normally.
  • For “.” explore all possible paths recursively.
  • The complexity remains O((26^2) * n), as there are at most two . in a word.

There are mainly 2 approach to solve this problem-

  1. Brute Force Method
  2. Depth First Search(Trie) Method

1. Brute Force Method

Words are stored in a list, and each search checks all words by iterating through them, including handling wildcards (.) character by character. This leads to a time complexity of O(m * n), where m is the number of words and n is the word length.

  • Time complexity: O(n) for addWord(), O(m∗n) for search().
  • Space complexity: O(m*n)

where m is the number of words added and n is the length of the string.

Code

2. Depth First Search(Trie) Method

Words are stored in a trie, and searches are done using depth-first search (DFS). Wildcards (.) are handled by recursively checking all possible matches at each position, improving search efficiency to O(n) time complexity, where n is the word length.

  • Time complexity: O(n) for addWord(), O(n) for search().
  • Space complexity: O(t+n)

where n is the length of the string and t is the total number of TrieNodes created in the Trie.

Code

More Articles