Skip to content

Commit 62e873a

Browse files
Add and Search Word - Data structure design: Accepted
1 parent 1f31562 commit 62e873a

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ My accepted leetcode solutions to some of the common interview problems.
9191
- [Design Search Autocomplete System](problems/src/design/AutocompleteSystem.java) (Hard)
9292
- [Design Excel Sum Formula](problems/src/design/Excel.java) (Hard)
9393
- [Flatten Nested List Iterator](problems/src/design/NestedIterator.java) (Medium)
94+
- [Add and Search Word - Data structure design](problems/src/design/WordDictionary.java) (Medium)
9495

9596
#### [Divide and Conquer](problems/src/divide_and_conquer)
9697

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package design;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by gouthamvidyapradhan on 09/12/2017.
8+
*
9+
* Design a data structure that supports the following two operations:
10+
11+
void addWord(word)
12+
bool search(word)
13+
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means
14+
it can represent any one letter.
15+
16+
For example:
17+
18+
addWord("bad")
19+
addWord("dad")
20+
addWord("mad")
21+
search("pad") -> false
22+
search("bad") -> true
23+
search(".ad") -> true
24+
search("b..") -> true
25+
Note:
26+
You may assume that all words are consist of lowercase letters a-z.
27+
28+
Solution: Implement a simple Trie and perform a search.
29+
*/
30+
public class WordDictionary {
31+
32+
private Trie trie;
33+
/**
34+
* Main method
35+
* @param args
36+
* @throws Exception
37+
*/
38+
public static void main(String[] args) throws Exception{
39+
WordDictionary wd = new WordDictionary();
40+
wd.addWord("bad");
41+
wd.addWord("dad");
42+
wd.addWord("mad");
43+
System.out.println(wd.search("pad"));
44+
System.out.println(wd.search("bad"));
45+
System.out.println(wd.search(".ad"));
46+
System.out.println(wd.search("..."));
47+
}
48+
49+
/** Initialize your data structure here. */
50+
public WordDictionary() {
51+
this.trie = new Trie();
52+
}
53+
54+
/** Adds a word into the data structure. */
55+
public void addWord(String word) {
56+
this.trie.insert(word);
57+
}
58+
59+
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any
60+
* one letter. */
61+
public boolean search(String word) {
62+
return this.trie.search(word);
63+
}
64+
65+
private class Trie {
66+
67+
private Map<Character, Trie> map;
68+
69+
/**
70+
* Initialize your data structure here.
71+
*/
72+
private Trie() {
73+
map = new HashMap<>();
74+
}
75+
76+
/**
77+
* Inserts a word into the trie.
78+
*/
79+
private void insert(String word) {
80+
if (word != null) {
81+
add(0, word, word.length());
82+
}
83+
}
84+
85+
private void add(int i, String word, int length) {
86+
if (i < length) {
87+
char c = word.charAt(i);
88+
Trie subTrie = map.get(c);
89+
if (subTrie == null) {
90+
subTrie = new Trie();
91+
map.put(c, subTrie);
92+
}
93+
subTrie.add(i + 1, word, length);
94+
} else map.put(null, new Trie()); //use null to indicate end of string
95+
}
96+
97+
/**
98+
* Returns if the word is in the trie.
99+
*/
100+
private boolean search(String word) {
101+
if (word != null) {
102+
return search(0, word, word.length());
103+
}
104+
return false;
105+
}
106+
107+
private boolean search(int i, String word, int length) {
108+
if (i < length) {
109+
char c = word.charAt(i);
110+
if(c == '.'){
111+
for(Character child : map.keySet()){
112+
if(child != null){
113+
Trie subTrie = map.get(child);
114+
if(subTrie.search(i + 1, word, length)) return true;
115+
}
116+
}
117+
return false;
118+
} else{
119+
Trie subTrie = map.get(c);
120+
if (subTrie == null)
121+
return false;
122+
return subTrie.search(i + 1, word, length);
123+
}
124+
}
125+
return map.containsKey(null);
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)