forked from gouthampradhan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordDictionary.java
128 lines (111 loc) · 3.5 KB
/
WordDictionary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package design;
import java.util.HashMap;
import java.util.Map;
/**
* Created by gouthamvidyapradhan on 09/12/2017.
*
* 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.
Solution: Implement a simple Trie and perform a search.
*/
public class WordDictionary {
private Trie trie;
/**
* Main method
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception{
WordDictionary wd = new WordDictionary();
wd.addWord("bad");
wd.addWord("dad");
wd.addWord("mad");
System.out.println(wd.search("pad"));
System.out.println(wd.search("bad"));
System.out.println(wd.search(".ad"));
System.out.println(wd.search("..."));
}
/** Initialize your data structure here. */
public WordDictionary() {
this.trie = new Trie();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
this.trie.insert(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 this.trie.search(word);
}
private class Trie {
private Map<Character, Trie> map;
/**
* Initialize your data structure here.
*/
private Trie() {
map = new HashMap<>();
}
/**
* Inserts a word into the trie.
*/
private void insert(String word) {
if (word != null) {
add(0, word, word.length());
}
}
private void add(int i, String word, int length) {
if (i < length) {
char c = word.charAt(i);
Trie subTrie = map.get(c);
if (subTrie == null) {
subTrie = new Trie();
map.put(c, subTrie);
}
subTrie.add(i + 1, word, length);
} else map.put(null, new Trie()); //use null to indicate end of string
}
/**
* Returns if the word is in the trie.
*/
private boolean search(String word) {
if (word != null) {
return search(0, word, word.length());
}
return false;
}
private boolean search(int i, String word, int length) {
if (i < length) {
char c = word.charAt(i);
if(c == '.'){
for(Character child : map.keySet()){
if(child != null){
Trie subTrie = map.get(child);
if(subTrie.search(i + 1, word, length)) return true;
}
}
return false;
} else{
Trie subTrie = map.get(c);
if (subTrie == null)
return false;
return subTrie.search(i + 1, word, length);
}
}
return map.containsKey(null);
}
}
}