diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md index 26ad75f4b4b3b..c6d3024f3d784 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README.md @@ -298,6 +298,102 @@ public: */ ``` +### **Rust** + +```rust +use std::{rc::Rc, cell::RefCell, collections::HashMap}; + +struct TrieNode { + pub val: Option<char>, + pub flag: bool, + pub child: HashMap<char, Rc<RefCell<TrieNode>>>, +} + +impl TrieNode { + fn new() -> Self { + Self { + val: None, + flag: false, + child: HashMap::new(), + } + } + + fn new_with_val(val: char) -> Self { + Self { + val: Some(val), + flag: false, + child: HashMap::new(), + } + } +} + +struct Trie { + root: Rc<RefCell<TrieNode>>, +} + +/// 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); +impl Trie { + fn new() -> Self { + Self { + root: Rc::new(RefCell::new(TrieNode::new())), + } + } + + fn insert(&self, word: String) { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + // We need to manually create the entry + root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new()))); + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + { + root.borrow_mut().flag = true; + } + } + + fn search(&self, word: String) -> bool { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + let flag = root.borrow().flag; + flag + } + + fn starts_with(&self, prefix: String) -> bool { + let char_vec: Vec<char> = prefix.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + true + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md index 79b840c2f4a17..7b6e1980da438 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/README_EN.md @@ -261,6 +261,102 @@ public: */ ``` +### **Rust** + +```rust +use std::{rc::Rc, cell::RefCell, collections::HashMap}; + +struct TrieNode { + pub val: Option<char>, + pub flag: bool, + pub child: HashMap<char, Rc<RefCell<TrieNode>>>, +} + +impl TrieNode { + fn new() -> Self { + Self { + val: None, + flag: false, + child: HashMap::new(), + } + } + + fn new_with_val(val: char) -> Self { + Self { + val: Some(val), + flag: false, + child: HashMap::new(), + } + } +} + +struct Trie { + root: Rc<RefCell<TrieNode>>, +} + +/// 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); +impl Trie { + fn new() -> Self { + Self { + root: Rc::new(RefCell::new(TrieNode::new())), + } + } + + fn insert(&self, word: String) { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + // We need to manually create the entry + root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new()))); + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + { + root.borrow_mut().flag = true; + } + } + + fn search(&self, word: String) -> bool { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + let flag = root.borrow().flag; + flag + } + + fn starts_with(&self, prefix: String) -> bool { + let char_vec: Vec<char> = prefix.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + true + } +} +``` + ### **Go** ```go diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs new file mode 100644 index 0000000000000..10e8ef6038c59 --- /dev/null +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.rs @@ -0,0 +1,91 @@ +use std::{rc::Rc, cell::RefCell, collections::HashMap}; + +struct TrieNode { + pub val: Option<char>, + pub flag: bool, + pub child: HashMap<char, Rc<RefCell<TrieNode>>>, +} + +impl TrieNode { + fn new() -> Self { + Self { + val: None, + flag: false, + child: HashMap::new(), + } + } + + fn new_with_val(val: char) -> Self { + Self { + val: Some(val), + flag: false, + child: HashMap::new(), + } + } +} + +struct Trie { + root: Rc<RefCell<TrieNode>>, +} + +/// 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); +impl Trie { + fn new() -> Self { + Self { + root: Rc::new(RefCell::new(TrieNode::new())), + } + } + + fn insert(&self, word: String) { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + // We need to manually create the entry + root.borrow_mut().child.insert(*c, Rc::new(RefCell::new(TrieNode::new()))); + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + { + root.borrow_mut().flag = true; + } + } + + fn search(&self, word: String) -> bool { + let char_vec: Vec<char> = word.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + let flag = root.borrow().flag; + flag + } + + fn starts_with(&self, prefix: String) -> bool { + let char_vec: Vec<char> = prefix.chars().collect(); + // Get the clone of current root node + let mut root = Rc::clone(&self.root); + for c in &char_vec { + if !root.borrow().child.contains_key(c) { + return false; + } + // Get the child node + let root_clone = Rc::clone(root.borrow().child.get(c).unwrap()); + root = root_clone; + } + true + } +} \ No newline at end of file