-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoComplete.java
More file actions
131 lines (109 loc) · 3.38 KB
/
AutoComplete.java
File metadata and controls
131 lines (109 loc) · 3.38 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
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
129
130
131
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AutoComplete {
class TreeNode {
HashMap<Character, TreeNode> children;
char cha;
boolean isWord;
TreeNode(char cha) {
this.cha = cha;
this.children = new HashMap<>();
this.isWord = isWord;
}
TreeNode() {
this.children = new HashMap<>();
}
void isWord() {
this.isWord = true;
}
}
TreeNode root;
/**
* Constructor
* @param library
*/
public AutoComplete(String[] library) {
this.root = new TreeNode();
for (String word: library) {
TreeNode cur = root;
char[] cArray = word.toCharArray();
for (int i = 0; i < cArray.length; i++) {
if (cur.children.get(cArray[i]) == null)
cur.children.put(cArray[i], new TreeNode(cArray[i]));
cur = cur.children.get(cArray[i]);
}
cur.isWord();
}
}
public List<String> complete(String prefix) {
List<String> res = new ArrayList<>();
char[] cArray = prefix.toCharArray();
TreeNode cur = root;
for (char c: cArray) {
if (cur == null) return res;
cur = cur.children.get(c);
}
completeHelper(res, new StringBuilder(prefix), cur);
return res;
}
//Depth first search helper, add all completed words into list
private void completeHelper(List<String> list, StringBuilder tmp, TreeNode cur) {
if (cur == null) return;
if (cur.isWord) {
list.add(tmp.toString());
}
for (TreeNode node: cur.children.values()) {
tmp.append(node.cha);
completeHelper(list, tmp, node);
tmp.delete(tmp.length() - 1, tmp.length());
}
}
public static void main(String[] args) {
String[] sArray = { "bat", "ball", "barrage", "barrier", "bell"};
String[] sArray2 = {
"whids",
"whyever",
"whiff",
"whiffable",
"whiffed",
"Whiffen",
"whiffenpoof",
"whiffer",
"whiffers",
"whiffet",
"whiffets",
"whiffy",
"whiffing",
"whiffle",
"whiffled",
"whiffler",
"whifflery",
"whiffleries",
"whifflers",
"whiffles",
"whiffletree",
"whiffletrees",
"whiffling",
"whifflingly",
"whiffs",
"whyfor",
"whift",
"Whig"
};
AutoComplete a = new AutoComplete(sArray2);
System.out.println(a.complete("whi"));
System.out.println(a.complete("ba"));
System.out.println(a.complete("be"));
System.out.println(a.complete("a"));
System.out.println(a.complete(""));
//fizz buzz:
// for (int i = 1; i <= 100; i++) {
//
// if (i % 5 == 0 && i % 3 == 0) System.out.println("fizz buzz");
// else if (i % 3 == 0) System.out.println("buzz");
// else if (i % 5 == 0) System.out.println("fizz");
// else System.out.println(i);
// }
}
}