211. Add and Search Word - Data structure design

题目:

Design a data structure that supports the following two operations:

void addWord(word)
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.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

Note:
You may assume that all words are consist of lowercase letters a-z.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem: Implement Trie (Prefix Tree) first.

链接: http://leetcode.com/problems/add-and-search-word-data-structure-design/

7/14/2017

58%,用Trie

参考508官方解答做的addWord,参考别人答案做的search,search部分主要是碰见'.'则遍历所有26个字母,是DFS,所以要把search分开,其中第二个可以用recursive写比较方便。

 1 public class WordDictionary {
 2     private static final int R = 26;
 3     private Node root;
 4     
 5     private static class Node {
 6         private boolean inWord;
 7         private Node[] next = new Node[R];
 8     }
 9     /** Initialize your data structure here. */
10     public WordDictionary() {
11         root = new Node();
12     }
13     
14     /** Adds a word into the data structure. */
15     public void addWord(String word) {
16         if (word == null || word.length() == 0) {
17             return;
18         }
19         Node node = root;
20         int d = 0;
21         while (d < word.length()) {
22             char c = word.charAt(d);
23             if(node.next[c - 'a'] == null) {
24                 node.next[c - 'a'] = new Node();
25             }
26             node = node.next[c - 'a'];
27             d++;
28         }
29         node.inWord = true;
30     }
31     
32     /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
33     public boolean search(String word) {
34         if (word == null || word.length() == 0) {
35             return false;
36         }
37         return search(root, word, 0);
38     }
39     private boolean search(Node x, String key, int d) {
40         if (x == null) {
41             return false;
42         }
43         if (d == key.length()) {
44             return x.inWord;
45         }
46         char c = key.charAt(d);
47         if (c == '.') {
48             for (Node node: x.next) {
49                 if (node != null && search(node, key, d + 1)) {
50                     return true;
51                 }
52             }
53             return false;
54         } else {
55             if (x.next[c - 'a'] == null) {
56                 return false;
57             }
58             return search(x.next[c - 'a'], key, d + 1);
59         }
60     }
61 }
62 
63 /**
64  * Your WordDictionary object will be instantiated and called as such:
65  * WordDictionary obj = new WordDictionary();
66  * obj.addWord(word);
67  * boolean param_2 = obj.search(word);
68  */

别人的答案

http://www.cnblogs.com/yrbbest/p/4979621.html

https://discuss.leetcode.com/topic/14216/tree-solutions-18-20-lines

更多讨论

https://discuss.leetcode.com/category/219/add-and-search-word-data-structure-design

posted @ 2017-07-15 11:43  panini  阅读(177)  评论(0编辑  收藏  举报