LeetCode: Add and Search Word - Data structure design

LeetCode: Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

1
2
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:

1
2
3
4
5
6
7
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.

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
public class WordDictionary {

private Node root;

/**
* Initialize your data structure here.
*/
public WordDictionary() {
root = new Node();
}

/**
* Adds a word into the data structure.
*/
public void addWord(String word) {
if (word == null || word.isEmpty()) return;
Map<Character, Node> children = root.children;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
Node node = children.getOrDefault(c, new Node());
node.val = c;
children.put(c, node);
children = node.children;
if (i == word.length() - 1) node.isLeaf = true;
}
}

/**
* 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 word == null || word.isEmpty() || helper(0, word, root);
}

private boolean helper(int start, String word, Node node) {
if (start == word.length()) return node.isLeaf;
char c = word.charAt(start);
boolean result = false;
if (c == '.') {
for (Map.Entry<Character, Node> entry : node.children.entrySet()) {
result = helper(start + 1, word, entry.getValue());
if (result) break;
}
} else {
if (node.children.containsKey(c)) {
result = helper(start + 1, word, node.children.get(c));
}
}
return result;
}

private static class Node {
char val;
boolean isLeaf = false;
Map<Character, Node> children = new HashMap<>();

}
}