-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathTrie.java
142 lines (111 loc) · 3.26 KB
/
Trie.java
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
132
133
134
135
136
137
138
139
140
141
142
package algo.string;
/**
* Created by sherxon on 2/2/17.
*/
/**
* This is prefix tree, which consumes a lot memory but very fast.
*/
@SuppressWarnings("Duplicates")
public class Trie {
TrieNode root = new TrieNode('0');
/**
* Initialize your data structure here.
*/
public Trie() {
}
/**
* Inserts a word into the trie.
*/
public void insert(String word) {
TrieNode t = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (t.children[index] == null)
t.children[index] = new TrieNode(word.charAt(i));
t = t.children[index];
}
t.isWord = true;
}
/**
* Returns if the word is in the trie.
*/
public boolean search(String word) {
TrieNode t = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (t.children[index] == null) return false;
t = t.children[index];
}
return t.isWord;
}
/**
* Returns if there is any word in the trie that starts with the given prefix.
*/
public boolean startsWith(String word) {
TrieNode t = root;
for (int i = 0; i < word.length(); i++) {
int index = word.charAt(i) - 'a';
if (t.children[index] == null) return false;
t = t.children[index];
}
return true;
}
private class TrieNode {
char val;
boolean isWord;
private TrieNode[] children = new TrieNode[26];
public TrieNode(char c) {
val = c;
}
}
}
/*
This is recursive, one-class approach, but it has small bug that I could not find it in 2 hours.
public class Trie {
int[] a= new int[26];
Trie[] children =new Trie[26];
boolean isWord=false;
*/
/**
* Initialize your data structure here. Inserts a word into the trie. Returns if the word is in the trie. Returns if there is any word in the trie that starts with the given prefix.
*//*
public Trie() {
}
*/
/** Inserts a word into the trie. *//*
public void insert(String word) {
insert(word, 0);
}
private void insert(String word, int i) {
int index=word.charAt(i)-'a';
a[index]++;
if(children[index]==null) children[index]=new Trie();
if(i==word.length()-1)
isWord=true;
else
children[index].insert(word, i + 1);
}
*/
/** Returns if the word is in the trie. *//*
public boolean search(String word) {
return search(word, 0);
}
private boolean search(String word, int i) {
int index=word.charAt(i)-'a';
if(a[index]==0)return false;
if(i==word.length()-1) return children[index].isWord;
return children[index].search(word, i + 1);
}
*/
/** Returns if there is any word in the trie that starts with the given prefix. *//*
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
private boolean startsWith(String prefix, int i) {
int index=prefix.charAt(i)-'a';
if(a[index]==0)return false;
if(i==prefix.length()-1) return true;
return children[index].startsWith(prefix, i + 1);
}
}
*/