Skip to content

Commit a47fdf5

Browse files
authored
feat: add rust solution to lc problem: No.0208 (#1328)
1 parent 7e09cb4 commit a47fdf5

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed

solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md

+96
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,102 @@ public:
298298
*/
299299
```
300300
301+
### **Rust**
302+
303+
```rust
304+
use std::{rc::Rc, cell::RefCell, collections::HashMap};
305+
306+
struct TrieNode {
307+
pub val: Option<char>,
308+
pub flag: bool,
309+
pub child: HashMap<char, Rc<RefCell<TrieNode>>>,
310+
}
311+
312+
impl TrieNode {
313+
fn new() -> Self {
314+
Self {
315+
val: None,
316+
flag: false,
317+
child: HashMap::new(),
318+
}
319+
}
320+
321+
fn new_with_val(val: char) -> Self {
322+
Self {
323+
val: Some(val),
324+
flag: false,
325+
child: HashMap::new(),
326+
}
327+
}
328+
}
329+
330+
struct Trie {
331+
root: Rc<RefCell<TrieNode>>,
332+
}
333+
334+
/// Your Trie object will be instantiated and called as such:
335+
/// let obj = Trie::new();
336+
/// obj.insert(word);
337+
/// let ret_2: bool = obj.search(word);
338+
/// let ret_3: bool = obj.starts_with(prefix);
339+
impl Trie {
340+
fn new() -> Self {
341+
Self {
342+
root: Rc::new(RefCell::new(TrieNode::new())),
343+
}
344+
}
345+
346+
fn insert(&self, word: String) {
347+
let char_vec: Vec<char> = word.chars().collect();
348+
// Get the clone of current root node
349+
let mut root = Rc::clone(&self.root);
350+
for c in &char_vec {
351+
if !root.borrow().child.contains_key(c) {
352+
// We need to manually create the entry
353+
root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new())));
354+
}
355+
// Get the child node
356+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
357+
root = root_clone;
358+
}
359+
{
360+
root.borrow_mut().flag = true;
361+
}
362+
}
363+
364+
fn search(&self, word: String) -> bool {
365+
let char_vec: Vec<char> = word.chars().collect();
366+
// Get the clone of current root node
367+
let mut root = Rc::clone(&self.root);
368+
for c in &char_vec {
369+
if !root.borrow().child.contains_key(c) {
370+
return false;
371+
}
372+
// Get the child node
373+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
374+
root = root_clone;
375+
}
376+
let flag = root.borrow().flag;
377+
flag
378+
}
379+
380+
fn starts_with(&self, prefix: String) -> bool {
381+
let char_vec: Vec<char> = prefix.chars().collect();
382+
// Get the clone of current root node
383+
let mut root = Rc::clone(&self.root);
384+
for c in &char_vec {
385+
if !root.borrow().child.contains_key(c) {
386+
return false;
387+
}
388+
// Get the child node
389+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
390+
root = root_clone;
391+
}
392+
true
393+
}
394+
}
395+
```
396+
301397
### **Go**
302398

303399
```go

solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md

+96
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,102 @@ public:
261261
*/
262262
```
263263
264+
### **Rust**
265+
266+
```rust
267+
use std::{rc::Rc, cell::RefCell, collections::HashMap};
268+
269+
struct TrieNode {
270+
pub val: Option<char>,
271+
pub flag: bool,
272+
pub child: HashMap<char, Rc<RefCell<TrieNode>>>,
273+
}
274+
275+
impl TrieNode {
276+
fn new() -> Self {
277+
Self {
278+
val: None,
279+
flag: false,
280+
child: HashMap::new(),
281+
}
282+
}
283+
284+
fn new_with_val(val: char) -> Self {
285+
Self {
286+
val: Some(val),
287+
flag: false,
288+
child: HashMap::new(),
289+
}
290+
}
291+
}
292+
293+
struct Trie {
294+
root: Rc<RefCell<TrieNode>>,
295+
}
296+
297+
/// Your Trie object will be instantiated and called as such:
298+
/// let obj = Trie::new();
299+
/// obj.insert(word);
300+
/// let ret_2: bool = obj.search(word);
301+
/// let ret_3: bool = obj.starts_with(prefix);
302+
impl Trie {
303+
fn new() -> Self {
304+
Self {
305+
root: Rc::new(RefCell::new(TrieNode::new())),
306+
}
307+
}
308+
309+
fn insert(&self, word: String) {
310+
let char_vec: Vec<char> = word.chars().collect();
311+
// Get the clone of current root node
312+
let mut root = Rc::clone(&self.root);
313+
for c in &char_vec {
314+
if !root.borrow().child.contains_key(c) {
315+
// We need to manually create the entry
316+
root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new())));
317+
}
318+
// Get the child node
319+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
320+
root = root_clone;
321+
}
322+
{
323+
root.borrow_mut().flag = true;
324+
}
325+
}
326+
327+
fn search(&self, word: String) -> bool {
328+
let char_vec: Vec<char> = word.chars().collect();
329+
// Get the clone of current root node
330+
let mut root = Rc::clone(&self.root);
331+
for c in &char_vec {
332+
if !root.borrow().child.contains_key(c) {
333+
return false;
334+
}
335+
// Get the child node
336+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
337+
root = root_clone;
338+
}
339+
let flag = root.borrow().flag;
340+
flag
341+
}
342+
343+
fn starts_with(&self, prefix: String) -> bool {
344+
let char_vec: Vec<char> = prefix.chars().collect();
345+
// Get the clone of current root node
346+
let mut root = Rc::clone(&self.root);
347+
for c in &char_vec {
348+
if !root.borrow().child.contains_key(c) {
349+
return false;
350+
}
351+
// Get the child node
352+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
353+
root = root_clone;
354+
}
355+
true
356+
}
357+
}
358+
```
359+
264360
### **Go**
265361

266362
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::{rc::Rc, cell::RefCell, collections::HashMap};
2+
3+
struct TrieNode {
4+
pub val: Option<char>,
5+
pub flag: bool,
6+
pub child: HashMap<char, Rc<RefCell<TrieNode>>>,
7+
}
8+
9+
impl TrieNode {
10+
fn new() -> Self {
11+
Self {
12+
val: None,
13+
flag: false,
14+
child: HashMap::new(),
15+
}
16+
}
17+
18+
fn new_with_val(val: char) -> Self {
19+
Self {
20+
val: Some(val),
21+
flag: false,
22+
child: HashMap::new(),
23+
}
24+
}
25+
}
26+
27+
struct Trie {
28+
root: Rc<RefCell<TrieNode>>,
29+
}
30+
31+
/// Your Trie object will be instantiated and called as such:
32+
/// let obj = Trie::new();
33+
/// obj.insert(word);
34+
/// let ret_2: bool = obj.search(word);
35+
/// let ret_3: bool = obj.starts_with(prefix);
36+
impl Trie {
37+
fn new() -> Self {
38+
Self {
39+
root: Rc::new(RefCell::new(TrieNode::new())),
40+
}
41+
}
42+
43+
fn insert(&self, word: String) {
44+
let char_vec: Vec<char> = word.chars().collect();
45+
// Get the clone of current root node
46+
let mut root = Rc::clone(&self.root);
47+
for c in &char_vec {
48+
if !root.borrow().child.contains_key(c) {
49+
// We need to manually create the entry
50+
root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new())));
51+
}
52+
// Get the child node
53+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
54+
root = root_clone;
55+
}
56+
{
57+
root.borrow_mut().flag = true;
58+
}
59+
}
60+
61+
fn search(&self, word: String) -> bool {
62+
let char_vec: Vec<char> = word.chars().collect();
63+
// Get the clone of current root node
64+
let mut root = Rc::clone(&self.root);
65+
for c in &char_vec {
66+
if !root.borrow().child.contains_key(c) {
67+
return false;
68+
}
69+
// Get the child node
70+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
71+
root = root_clone;
72+
}
73+
let flag = root.borrow().flag;
74+
flag
75+
}
76+
77+
fn starts_with(&self, prefix: String) -> bool {
78+
let char_vec: Vec<char> = prefix.chars().collect();
79+
// Get the clone of current root node
80+
let mut root = Rc::clone(&self.root);
81+
for c in &char_vec {
82+
if !root.borrow().child.contains_key(c) {
83+
return false;
84+
}
85+
// Get the child node
86+
let root_clone = Rc::clone(root.borrow().child.get(c).unwrap());
87+
root = root_clone;
88+
}
89+
true
90+
}
91+
}

0 commit comments

Comments
 (0)