public TrieNode(char val) {
childNode = new TrieNode[26];
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode(' ');
/** Adds a word into the data structure. */
public void addWord(String word) {
if (word == null || word.length() == 0) {
for (char c : word.toCharArray()) {
if (curNode.childNode[index] == null) {
curNode.childNode[index] = new TrieNode(c);
curNode = curNode.childNode[index];
/** 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) {
if (word == null || word.length() == 0) {
return searchByDFS(root, 0, word);
public boolean searchByDFS(TrieNode node, int depth, String word) {
if (depth == word.length()) {
char c = word.charAt(depth);
for (TrieNode childNode : node.childNode) {
if (childNode != null && searchByDFS(childNode, depth + 1, word)) {
return node.childNode[index] != null && searchByDFS(node.childNode[index], depth + 1, word);
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* boolean param_2 = obj.search(word);