# 0211. Design Add and Search Words Data Structure ###### tags: `Leetcode` `Medium` `FaceBook` `Trie` Link: https://leetcode.com/problems/design-add-and-search-words-data-structure/ ## 思路 典型的trie 遇到.就dfs recursive遍历 ## Code ```java= class WordDictionary { class TrieNode{ TrieNode[] children = new TrieNode[26]; boolean isWord = false; } TrieNode root; public WordDictionary() { root = new TrieNode(); } public void addWord(String word) { TrieNode curr = root; for(int i = 0;i < word.length();i++){ if(curr.children[word.charAt(i)-'a']==null){ curr.children[word.charAt(i)-'a'] = new TrieNode(); } curr = curr.children[word.charAt(i)-'a']; } curr.isWord = true; } public boolean search(String word) { TrieNode curr = root; return helper(word, curr, 0); } public boolean helper(String word, TrieNode curr, int idx){ for(int i = idx;i < word.length();i++){ if(word.charAt(i)!='.'){ if(curr.children[word.charAt(i)-'a']==null) return false; curr = curr.children[word.charAt(i)-'a']; } else{ for(char c = 'a';c <= 'z';c++){ if(curr.children[c-'a']!=null){ if(helper(word, curr.children[c-'a'], i+1)) return true; } } return false; } } return curr.isWord; } } ```