- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoComplete.java
More file actions
Latest commit
131 lines (109 loc) · 3.38 KB
/
Copy pathAutoComplete.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
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
publicclassAutoComplete {
classTreeNode {
HashMap<Character, TreeNode> children;
charcha;
booleanisWord;
TreeNode(charcha) {
this.cha = cha;
this.children = newHashMap<>();
this.isWord = isWord;
}
TreeNode() {
this.children = newHashMap<>();
}
voidisWord() {
this.isWord = true;
}
}
TreeNoderoot;
/**
* Constructor
* @param library
*/
publicAutoComplete(String[] library) {
this.root = newTreeNode();
for (Stringword: library) {
TreeNodecur = root;
char[] cArray = word.toCharArray();
for (inti = 0; i < cArray.length; i++) {
if (cur.children.get(cArray[i]) == null)
cur.children.put(cArray[i], newTreeNode(cArray[i]));
cur = cur.children.get(cArray[i]);
}
cur.isWord();
}
}
publicList<String> complete(Stringprefix) {
List<String> res = newArrayList<>();
char[] cArray = prefix.toCharArray();
TreeNodecur = root;
for (charc: cArray) {
if (cur == null) returnres;
cur = cur.children.get(c);
}
completeHelper(res, newStringBuilder(prefix), cur);
returnres;
}
//Depth first search helper, add all completed words into list
privatevoidcompleteHelper(List<String> list, StringBuildertmp, TreeNodecur) {
if (cur == null) return;
if (cur.isWord) {
list.add(tmp.toString());
}
for (TreeNodenode: cur.children.values()) {
tmp.append(node.cha);
completeHelper(list, tmp, node);
tmp.delete(tmp.length() - 1, tmp.length());
}
}
publicstaticvoidmain(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"
};
AutoCompletea = newAutoComplete(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);
// }
}
}