BiruLyu
6/24/2017 - 6:21 AM

211. Add and Search Word - Data structure design(Array).java

class TrieNode {
    Map<Character, TrieNode> map;
    boolean hasWord;
    public TrieNode () {
        map = new HashMap<Character, TrieNode>();
        hasWord = false;
    }
}
public class WordDictionary {
    
    private TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode cur = root;
        for (char c : word.toCharArray()) {
            if (!cur.map.containsKey(c)) {
                cur.map.put(c, new TrieNode());
            }
            cur = cur.map.get(c);
        }
        cur.hasWord = true;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        return backtracking(word.toCharArray(), 0, root);
    }
    
    public boolean backtracking(char[] word, int start, TrieNode node) {
        if (start == word.length) return node.hasWord;
        if (word[start] != '.') {
            return node.map.containsKey(word[start]) && backtracking (word, start + 1, node.map.get(word[start]));
        } else {
            for (Map.Entry<Character, TrieNode> entry : node.map.entrySet()) {
                if (backtracking(word, start + 1, entry.getValue())) {
                    return true;
                }
            }
            return false;
        }
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */
public class WordDictionary {

    /** Initialize your data structure here. */
    TrieNode root;
    public WordDictionary() {
        root = new TrieNode();
    }
    
    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode ws = root;
        for (int i = 0; i < word.length(); i++){
            char c = word.charAt(i);
            if (ws.children[c - 'a'] == null){
                ws.children[c - 'a'] = new TrieNode();
            }
            ws = ws.children[c - 'a'];
        }
        ws.isword = true;
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    public boolean search(String word) {
        char[] s = word.toCharArray();
        TrieNode ws = root;
        return helper(s, 0, ws);
    }
    
    private boolean helper(char[] s, int j, TrieNode ws){
        if (j >= s.length){
            return ws.isword;
        }
        
        if(s[j] != '.'){
            if(ws.children[s[j]-'a'] == null){
                return false;
            }
            else{
                return helper(s, j+1, ws.children[s[j]-'a']);
            }
        }
        else{
            for(char c = 'a'; c <= 'z'; c++){
                if(ws.children[c-'a'] !=null){
                    if(helper(s, j+1, ws.children[c-'a'])){
                        return true;
                    }
                }
            }
            return false;
        }
    }
}

class TrieNode{
    boolean isword;
    TrieNode[] children;
    public TrieNode(){
        children = new TrieNode[26];
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */