Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
52 lines (45 loc) · 1.51 KB
/
Copy pathSolution.java
File metadata and controls
52 lines (45 loc) · 1.51 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
// Stepsort · Implement Trie (Prefix Tree)
// Category: LeetCode Patterns
// Animated walkthrough: https://stepsort.prakashraj.me/algorithm/implement-trie
public class Main {
static class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean isEnd;
}
static class Trie {
private final TrieNode root = new TrieNode();
void insert(String word) {
TrieNode node = root;
for (char ch : word.toCharArray()) {
int idx = ch - 'a';
if (node.children[idx] == null) node.children[idx] = new TrieNode();
node = node.children[idx];
}
node.isEnd = true;
}
private TrieNode find(String s) {
TrieNode node = root;
for (char ch : s.toCharArray()) {
node = node.children[ch - 'a'];
if (node == null) return null;
}
return node;
}
boolean search(String word) {
TrieNode node = find(word);
return node != null && node.isEnd;
}
boolean startsWith(String prefix) {
return find(prefix) != null;
}
}
public static void main(String[] args) {
Trie trie = new Trie();
trie.insert("apple");
System.out.println(trie.search("apple"));
System.out.println(trie.search("app"));
System.out.println(trie.startsWith("app"));
trie.insert("app");
System.out.println(trie.search("app"));
}
}