LeetCode 211: Design Add and Search Words Data Structure Solution

Master LeetCode problem 211 (Design Add and Search Words Data Structure), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

211. Design Add and Search Words Data Structure

Problem Explanation

Explanation:

To solve this problem, we can use a Trie data structure to store the words. When adding a word, we traverse the Trie character by character and create new nodes if necessary. When searching for a word, we can use depth-first search (DFS) to match the characters of the word with the Trie nodes. If we encounter a dot ('.'), we need to explore all possible paths.

Algorithm:

  1. Implement a TrieNode class with a HashMap to store children nodes.
  2. Implement a WordDictionary class with methods to add a word and search for a word.
  3. When adding a word, traverse the Trie and create nodes as needed.
  4. When searching for a word, perform DFS with backtracking to match the characters with the Trie nodes. If a dot is encountered, explore all possible paths.
  5. Return true if a matching word is found, false otherwise.

Time Complexity:

  • Adding a word: O(L), where L is the length of the word.
  • Searching for a word: O(26^M), where M is the number of dots in the search word.

Space Complexity:

  • O(N), where N is the total number of characters in all added words.

:

Solution Code

class TrieNode {
    HashMap<Character, TrieNode> children;
    boolean isEnd;

    public TrieNode() {
        children = new HashMap<>();
        isEnd = false;
    }
}

class WordDictionary {
    private TrieNode root;

    public WordDictionary() {
        root = new TrieNode();
    }

    public void addWord(String word) {
        TrieNode node = root;
        for (char c : word.toCharArray()) {
            if (!node.children.containsKey(c)) {
                node.children.put(c, new TrieNode());
            }
            node = node.children.get(c);
        }
        node.isEnd = true;
    }

    public boolean search(String word) {
        return searchWord(word, 0, root);
    }

    private boolean searchWord(String word, int index, TrieNode node) {
        if (index == word.length()) {
            return node.isEnd;
        }

        char c = word.charAt(index);
        if (c == '.') {
            for (char key : node.children.keySet()) {
                if (searchWord(word, index + 1, node.children.get(key))) {
                    return true;
                }
            }
            return false;
        } else {
            if (node.children.containsKey(c)) {
                return searchWord(word, index + 1, node.children.get(c));
            } else {
                return false;
            }
        }
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 211 (Design Add and Search Words Data Structure)?

This page provides optimized solutions for LeetCode problem 211 (Design Add and Search Words Data Structure) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 211 (Design Add and Search Words Data Structure)?

The time complexity for LeetCode 211 (Design Add and Search Words Data Structure) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 211 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 211 in Java, C++, or Python.

Back to LeetCode Solutions