package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/add-and-search-word-data-structure-design/
 */
public class Problem211 {
    public static class WordDictionary {
        private Node root = new Node();

        private static class Node {
            char ch;
            boolean done;
            Map<Character, Node> children = new HashMap<>();

            public Node() {
            }

            public Node(char ch, boolean done) {
                this.ch = ch;
                this.done = done;
            }
        }

        // Adds a word into the data structure.
        public void addWord(String word) {
            addWord(root, word, 0);
        }

        private void addWord(Node node, String word, int idx) {
            if (idx == word.length()) {
                node.done = true;
                return;
            }
            char ch = word.charAt(idx);
            Node n;
            if (node.children.containsKey(ch)) {
                n = node.children.get(ch);
            } else {
                n = new Node(ch, false);
                node.children.put(ch, n);
            }
            addWord(n, word, idx + 1);
        }

        // 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 search(root, word, 0);
        }

        private boolean search(Node node, String word, int idx) {
            if (idx == word.length()) {
                return node.done;
            }
            char ch = word.charAt(idx);
            if (!node.children.containsKey(ch)) {
                if (ch == '.') {
                    boolean found = false;
                    for (Map.Entry<Character, Node> e : node.children.entrySet()) {
                        found |= search(e.getValue(), word, idx + 1);
                    }
                    return found;
                }
                return false;
            }
            Node n = node.children.get(ch);
            return search(n, word, idx + 1);
        }
    }
}

@@ -16,57 +16,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -179,16 +179,22 @@
 private 
+final 
 Node roo
@@ -254,24 +254,32 @@
             
+private 
 char ch;%0A   
@@ -279,32 +279,40 @@
 ch;%0A            
+private 
 boolean done;%0A  
@@ -325,28 +325,28 @@
     
-Map%3CCharacter,
+private final
  Node
-%3E
+%5B%5D
  chi
@@ -361,19 +361,16 @@
 new 
-HashMap%3C%3E()
+Node%5B26%5D
 ;%0A%0A 
@@ -703,16 +703,23 @@
 private 
+static 
 void add
@@ -932,62 +932,36 @@
 de n
-;%0A            if (node.children.containsKey(ch)) %7B%0A   
+ = node.children%5Bch - 'a'%5D;%0A
     
@@ -972,54 +972,22 @@
     
- 
+if (
 n =
+=
  n
-ode.children.get(ch);%0A            %7D else
+ull)
  %7B%0A 
@@ -1059,19 +1059,22 @@
 dren
-.put(ch,
+%5Bch - 'a'%5D =
  n
-)
 ;%0A  
@@ -1381,32 +1381,39 @@
         private 
+static 
 boolean search(N
@@ -1578,62 +1578,8 @@
 x);%0A
-            if (!node.children.containsKey(ch)) %7B%0A    
     
@@ -1599,28 +1599,24 @@
 h == '.') %7B%0A
-    
             
@@ -1662,80 +1662,140 @@
     
-    
 for (
-Map.Entry%3CCharacter, Node%3E e : node.children.entrySet()) %7B%0A    
+Node child : node.children) %7B%0A                    if (child == null) %7B%0A                        continue;%0A                    %7D%0A
     
@@ -1830,20 +1830,13 @@
 rch(
-e.getValue()
+child
 , wo
@@ -1861,34 +1861,26 @@
             
-  
-  %7D%0A    
+%7D%0A
             
@@ -1901,37 +1901,84 @@
 nd;%0A            
+%7D%0A
     
-%7D
+        if (node.children%5Bch - 'a'%5D == null) %7B
 %0A               
@@ -2044,16 +2044,18 @@
 dren
-.get(ch)
+%5Bch - 'a'%5D
 ;%0A  

Update problem 211