-
Notifications
You must be signed in to change notification settings - Fork 31
/
0208-implement-trie-prefix-tree.rs
143 lines (125 loc) · 4.18 KB
/
0208-implement-trie-prefix-tree.rs
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
143
/*
Problem: LeetCode 208 - Implement Trie (Prefix Tree)
Key Idea:
The key idea is to use a Trie data structure to efficiently store and search for words with common prefixes.
Approach:
1. Create a struct `TrieNode` to represent a node in the Trie. Each node contains:
- A boolean flag `is_end` to indicate whether the current node marks the end of a word.
- An array of 26 elements (one for each lowercase letter) to store child nodes.
2. Create a struct `Trie` that represents the Trie data structure. It contains a single field `root`, which is the root node of the Trie.
3. Implement the following methods for the `Trie`:
- `new()`: Initialize an empty Trie with a root node.
- `insert(word: String)`: Insert a word into the Trie by iterating through its characters and creating nodes as necessary.
- `search(word: String) -> bool`: Search for a word in the Trie by traversing nodes and checking the `is_end` flag.
- `starts_with(prefix: String) -> bool`: Check if any word in the Trie starts with the given prefix.
Time Complexity:
- Insertion, search, and starts_with operations all have a time complexity of O(L), where L is the length of the word or prefix being processed.
Space Complexity:
- The space complexity is O(M * L), where M is the number of words inserted, and L is the average length of words. Each character in a word occupies space in the Trie.
*/
struct TrieNode {
is_end: bool,
children: [Option<Box<TrieNode>>; 26],
}
impl TrieNode {
fn new() -> Self {
TrieNode {
is_end: false,
children: Default::default(),
}
}
}
pub struct Trie {
root: TrieNode,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl Trie {
pub fn new() -> Self {
Trie { root: TrieNode::new() }
}
pub fn insert(&mut self, word: String) {
let mut node = &mut self.root;
for c in word.chars() {
let index = (c as u8 - b'a') as usize;
node = node.children[index].get_or_insert_with(|| Box::new(TrieNode::new()));
}
node.is_end = true;
}
pub fn search(&self, word: String) -> bool {
let mut node = &self.root;
for c in word.chars() {
let index = (c as u8 - b'a') as usize;
if let Some(next) = &node.children[index] {
node = next;
} else {
return false;
}
}
node.is_end
}
pub fn starts_with(&self, prefix: String) -> bool {
let mut node = &self.root;
for c in prefix.chars() {
let index = (c as u8 - b'a') as usize;
if let Some(next) = &node.children[index] {
node = next;
} else {
return false;
}
}
true
}
}
/**
* Your Trie object will be instantiated and called as such:
* let obj = Trie::new();
* obj.insert(word);
* let ret_2: bool = obj.search(word);
* let ret_3: bool = obj.starts_with(prefix);
**/
/**
struct Trie {
children: HashMap<char, Trie>,
is_end: bool,
}
impl Trie {
fn new() -> Self {
Trie {
children: HashMap::new(),
is_end: false,
}
}
fn insert(&mut self, word: String) {
let mut node = self;
for c in word.chars() {
node = node.children.entry(c).or_insert(Trie::new());
}
node.is_end = true;
}
fn search(&self, word: String) -> bool {
let mut node = self;
for c in word.chars() {
if let Some(next_node) = node.children.get(&c) {
node = next_node;
} else {
return false;
}
}
node.is_end
}
fn starts_with(&self, prefix: String) -> bool {
let mut node = self;
for c in prefix.chars() {
if let Some(next_node) = node.children.get(&c) {
node = next_node;
} else {
return false;
}
}
true
}
}
**/