-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.java
More file actions
54 lines (48 loc) · 1.5 KB
/
Trie.java
File metadata and controls
54 lines (48 loc) · 1.5 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
public class Trie
{
static class Node {
Node[] children;
boolean eow;
public Node() {
children = new Node[26];
eow = false;
}
}
static Node root = new Node();
public static void insert(String word) {
Node curr = root;
for(int i=0; i<word.length();i++) {
int idx = word.charAt(i) - 'a';
if(curr.children[idx] == null) curr.children[idx] = new Node();
if(i == word.length()-1) curr.eow = true;
curr = curr.children[idx];
}
}
public static boolean search(String word) {
Node curr = root;
for(int i=0; i<word.length();i++) {
int idx = word.charAt(i) - 'a';
if(curr.children[idx] == null) return false;
if(i == word.length()-1 && !curr.eow) return false;
curr = curr.children[idx];
}
return true;
}
public static boolean wordBroken(String word) {
if(word.isEmpty()) return true;
for(int i=1; i<=word.length(); i++) {
boolean a = search(word.substring(0, i));
boolean b = wordBroken(word.substring(i));
if(a && b) return true;
}
return false;
}
public static void main(String[] args) {
String[] words = {"the", "a", "there", "their", "any"};
for(String word: words) {
insert(word);
}
System.out.println(search("an"));
System.out.println(wordBroken("thether"));
}
}