[LeetCode] 211. Design Add and Search Words Data Structure
Design a data structure that supports adding new words and finding if a string matches any previously added string.
Implement the WordDictionary
class:
WordDictionary()
Initializes the object.void addWord(word)
Addsword
to the data structure, it can be matched later.bool search(word)
Returnstrue
if there is any string in the data structure that matchesword
orfalse
otherwise.word
may contain dots'.'
where dots can be matched with any letter.
Example:
Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] Output [null,null,null,null,false,true,true,true] Explanation WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("b.."); // return True
Constraints:
1 <= word.length <= 500
word
inaddWord
consists lower-case English letters.word
insearch
consist of'.'
or lower-case English letters.- At most
50000
calls will be made toaddWord
andsearch
.
添加和查找单词 - 数据结构设计。
请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
实现词典类 WordDictionary :
WordDictionary() 初始化词典对象
void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 '.' ,每个 . 都可以表示任何一个字母。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/design-add-and-search-words-data-structure
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题意是设计两个函数,其中点代表通配符,什么字母都可以。这是一道典型的字典树(Trie)的题。同样的设计题有208题,几乎一样。字典树又叫做前缀树。字典树会为每个位置上的字母创建一个长度为26的数组,以判断当前位置上到底是哪个字母。
当加入单词(addWord)的时候,遍历input单词的每个字母,如果当前位置上还未创建新的TrieNode,则创建。遍历完了记得改变flag和记录当前单词是什么。
当查找单词(search)的时候,需要用DFS的方式递归寻找单词。遍历单词的每个字母,判断当前字母是否在字典树中出现。
时间O(n), n 是需要遍历的单词的长度
空间O(num of TrieNode * 26) = O(num of words * word.length() * 26)
Java实现
1 class WordDictionary { 2 class Node { 3 Node[] children; 4 boolean isEnd; 5 String word; 6 7 public Node() { 8 children = new Node[26]; 9 isEnd = false; 10 word = ""; 11 } 12 } 13 14 Node root; 15 16 public WordDictionary() { 17 root = new Node(); 18 } 19 20 public void addWord(String word) { 21 Node node = root; 22 for (int i = 0; i < word.length(); i++) { 23 int j = word.charAt(i) - 'a'; 24 if (node.children[j] == null) { 25 node.children[j] = new Node(); 26 } 27 node = node.children[j]; 28 } 29 node.isEnd = true; 30 // node.word = word; 31 } 32 33 public boolean search(String word) { 34 return helper(word, root, 0); 35 } 36 37 private boolean helper(String word, Node node, int index) { 38 if (index == word.length()) { 39 return node.isEnd; 40 } 41 if (word.charAt(index) == '.') { 42 for (Node child : node.children) { 43 if (child != null && helper(word, child, index + 1)) { 44 return true; 45 } 46 } 47 return false; 48 } else { 49 int j = word.charAt(index) - 'a'; 50 Node child = node.children[j]; 51 return child != null && helper(word, child, index + 1); 52 } 53 } 54 } 55 56 /** 57 * Your WordDictionary object will be instantiated and called as such: 58 * WordDictionary obj = new WordDictionary(); 59 * obj.addWord(word); 60 * boolean param_2 = obj.search(word); 61 */