Design Add and Search Words Data Structure

 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","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

wordDictionary.search("b.."); // return True

 


Constraints:


1 <= word.length <= 25

word in addWord consists of lowercase English letters.

word in search consist of '.' or lowercase English letters.

There will be at most 2 dots in word for search queries.

At most 104 calls will be made to addWord and search.


class WordDictionary {

    // Root of the trie
    private TrieNode root;

    // TrieNode class
    private class TrieNode {
        TrieNode[] children = new TrieNode[26];
        boolean isEndOfWord = false;
    }

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

    // Add word into the trie
    public void addWord(String word) {
        TrieNode curr = root;

        for (char ch : word.toCharArray()) {
            int index = ch - 'a';
            if (curr.children[index] == null) {
                curr.children[index] = new TrieNode();
            }
            curr = curr.children[index];
        }

        curr.isEndOfWord = true;
    }

    // Search word (with '.' support)
    public boolean search(String word) {
        return searchHelper(word, root);
    }

   
    private boolean searchHelper(String word, TrieNode root) {
        TrieNode curr = root;

        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);

            if (ch == '.') {
                for (TrieNode child : curr.children) {
                    if (child != null && searchHelper(word.substring(i + 1), child)) {
                        return true;
                    }
                }
                return false;
            } else {
                int index = ch - 'a';
                if (curr.children[index] == null) {
                    return false;
                }
                curr = curr.children[index];
            }
        }

        return curr.isEndOfWord;
    }
}

Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence