码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Add and Search Word - Data structure design

时间:2015-12-17 06:58:41      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

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.

This is related to previous problem: Implement Trie

Only because in this problem, "." is introduced, which will cause us to backtrack to check each son. Using iteration is not that convient, we use recursion here.

One thing to be cautious is that when inserting words to a trie, the last node should have node.isEnd set to be true. Always forget this

 1 public class WordDictionary {
 2     public class TrieNode {
 3         int num; //count how many words go through this TrieNode
 4         TrieNode[] sons; //Arrays of children
 5         boolean isEnd;
 6         char val;
 7         
 8         public TrieNode() {
 9             this.num = 0;
10             this.sons = new TrieNode[26];
11             this.isEnd = false;
12         }
13     }
14     
15     TrieNode root;
16     
17     public WordDictionary() {
18         this.root = new TrieNode();
19     }
20 
21     // Adds a word into the data structure.
22     public void addWord(String word) {
23         if (word==null || word.length()==0) return;
24         char[] arr = word.toCharArray();
25         TrieNode node = this.root;
26         for (char c : arr) {
27             int pos = (int)(c - ‘a‘);
28             if (node.sons[pos] == null) {
29                 node.sons[pos] = new TrieNode();
30                 node.sons[pos].num = 1;
31                 node.sons[pos].val = c;
32             }
33             else {
34                 node.sons[pos].num++;
35             }
36             node = node.sons[pos];
37         }
38         node.isEnd = true;
39     }
40 
41     // Returns if the word is in the data structure. A word could
42     // contain the dot character ‘.‘ to represent any one letter.
43     public boolean search(String word) {
44         int len = word.length();
45         TrieNode node = this.root;
46         return search(word, node, 0, len);
47     }
48     
49     public boolean search(String word, TrieNode node, int cur, int len) {
50         if (cur == len) return node.isEnd;
51         char w = word.charAt(cur);
52         if (w == ‘.‘) {
53             for (int j=0; j<26; j++) {
54                 if (node.sons[j] != null) {
55                     if (search(word, node.sons[j], cur+1, len))
56                         return true;
57                 }
58             }
59             return false;
60         }
61         else {
62             int pos = (int)(w - ‘a‘);
63             if (node.sons[pos] == null) return false;
64             return search(word, node.sons[pos], cur+1, len);
65         }
66     }
67 }
68 
69 // Your WordDictionary object will be instantiated and called as such:
70 // WordDictionary wordDictionary = new WordDictionary();
71 // wordDictionary.addWord("word");
72 // wordDictionary.search("pattern");

 

Leetcode: Add and Search Word - Data structure design

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5052887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!