# 211. Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

Example:

search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.

## Solution

1. 字典树Node的定义
2. item存在的意义：识别在路径中，但却没有定义过的情况

## Code

``````class WordDictionary {

class TrieNode{
TrieNode[] children = new TrieNode[26];
String item = "";
}

private TrieNode root;
/** Initialize your data structure here. */
public WordDictionary() {
root = new TrieNode();
}

/** Adds a word into the data structure. */
TrieNode node = root;
for(Character c : word.toCharArray()){
if(node.children[c-'a'] == null){
node.children[c-'a'] = new TrieNode();
}
node = node.children[c-'a'];
}
node.item = word;
}

/** 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 match(word.toCharArray(), 0, root);
}

public boolean match(char[] arr, int k, TrieNode node){
if(k == arr.length){
return !node.item.equals("");
}

if(arr[k] == '.'){
for(int i = 0; i < node.children.length;i++){
if(node.children[i] != null && match(arr, k+1, node.children[i]))
return true;
}
}else{
return node.children[arr[k]-'a'] != null && match(arr, k+1, node.children[arr[k]-'a']);
}
return false;
}

}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();