forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplementTrie.java
76 lines (65 loc) · 2.45 KB
/
ImplementTrie.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
//Trie better than hashtable because gives us the ability to
//1. Find ALL keys with a given prefix
//2. Go through a dataset of strings in lexographical order
//3. Uses less space because as hash table increases in size could be a lot of collisions and search time
//could deteriorate to O(N)
//[c-'a']
//is a way to get the position of the character in the alphabet. 'a' - 'a' would give you 0. 'b' - 'a' would give you 1. 'c' - 'a' would give you 2, and so on.
//TC: INSERT: O(N) where n is length of word, we iteratethrough the entire length of word
// SEARCH: O(N) where n is length of word, we will have to go through each char in input word
// STARTSWITH: O(M) where m is length of prefix
//SC: INSERT: O(N) where n is length of word, in worst case, we need to create N new nodes, for a
// word that is completely new and not in our Trie!
// SEARCH: O(1) we dont need any extra space since trie is already created
// STARTSWITH: O(1) we dont need any extra space since trie is created
class TrieNode {
boolean isEndOfWord;
TrieNode[] children;
// Initialize your data structure here.
public TrieNode() {
this.isEndOfWord = false;
this.children = new TrieNode[26];
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode runner = root;
for(char c : word.toCharArray()){
if(runner.children[c-'a'] == null) {
runner.children[c-'a'] = new TrieNode();
}
runner = runner.children[c-'a'];
}
runner.isEndOfWord = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode runner = root;
for(char c : word.toCharArray()) {
if(runner.children[c-'a'] == null) {
return false;
} else {
runner = runner.children[c-'a'];
}
}
return runner.isEndOfWord;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode runner = root;
for(char c : prefix.toCharArray()) {
if(runner.children[c-'a'] == null) {
return false;
} else {
runner = runner.children[c-'a'];
}
}
return true;
}
}