-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddandSearchWordDatastructuredesign.java
More file actions
91 lines (86 loc) · 2.54 KB
/
AddandSearchWordDatastructuredesign.java
File metadata and controls
91 lines (86 loc) · 2.54 KB
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
package medium;
/**
* ClassName: AddandSearchWordDatastructuredesign.java
* Auther: chenyiAlone
* Create Time: 2019/5/8 17:15
* Description: No.211
* 思路:
* 1. use boolean check() to DFS tree
* 2. DFS
* if c = '.'
* for i = 0 to 25
* if root.edges[i] != null && check()
* return true
* return false
* else
* if root.edges[c] != null
* return check(root.edges[c])
*
*
*
*
* 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.
*
* 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.
*
*/
public class AddandSearchWordDatastructuredesign {
private class Node {
boolean isWord = false;
Node[] edges;
private Node() {
edges = new Node[26];
}
}
private Node root;
/** Initialize your data structure here. */
public AddandSearchWordDatastructuredesign() {
root = new Node();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
if ("".equals(word))
return;
Node iter = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (iter.edges[index] == null) {
iter.edges[index] = new Node();
}
iter = iter.edges[index];
}
iter.isWord = true;
}
/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
private boolean check(int index, char[] word, Node root) {
if (index == word.length)
return root.isWord;
if (word[index] == '.') {
for (int i = 0; i < 26; i++)
if (root.edges[i] != null && check(index + 1, word, root.edges[i]))
return true;
} else {
int p = word[index] - 'a';
if (root.edges[p] != null)
return check(index + 1, word, root.edges[p]);
}
return false;
}
public boolean search(String word) {
return check(0, word.toCharArray(), root);
}
}