[−][src]Struct leetcode_for_rust::cd0208_implement_trie_prefix_tree::Trie
Solutions
Approach 1: Iterative
-
Time complexity: O(n)
-
Space complexity: O(1)
#[derive(Default)] struct Trie { is_end: bool, nodes: [Option<Box<Trie>>; 26], } /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl Trie { /** Initialize your data structure here. */ fn new() -> Self { Default::default() } /** Inserts a word into the trie. */ fn insert(&mut self, word: String) { let mut curr = self; for i in word.chars().map(|char| (char as u8 - 'a' as u8) as usize) { curr = curr.nodes[i].get_or_insert_with(|| Box::new(Trie::new())); } curr.is_end = true; } /** Returns if the word is in the trie. */ fn search(&self, word: String) -> bool { let mut curr = self; for i in word.chars().map(|char| (char as u8 - 'a' as u8) as usize) { match curr.nodes[i].as_ref() { Some(node) => { curr = node; }, None => { return false; }, } } curr.is_end } /** Returns if there is any word in the trie that starts with the given prefix. */ fn starts_with(&self, prefix: String) -> bool { let mut curr = self; for i in prefix.chars().map(|char| (char as u8 - 'a' as u8) as usize) { match curr.nodes[i].as_ref() { Some(node) => { curr = node; }, None => { 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); */
Approach 2: Iterative
-
Time complexity: O(n)
-
Space complexity: O(1)
#[derive(Default)] struct Trie { is_end: bool, nodes: [Option<Box<Trie>>; 26], } /** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */ impl Trie { /** Initialize your data structure here. */ fn new() -> Self { Default::default() } /** Returns if the word is in the trie. */ fn search(&self, word: String) -> bool { self.find(word).map_or(false, |t| t.is_end) } /** Returns if there is any word in the trie that starts with the given prefix. */ fn starts_with(&self, prefix: String) -> bool { self.find(prefix).is_some() } fn find(&self, word: String) -> Option<&Trie> { let mut curr = self; for i in word.chars().map(|ch| (ch as u8 - 'a' as u8) as usize) { curr = curr.nodes[i].as_ref()?; } Some(curr) } } /** * 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); */
Trait Implementations
Auto Trait Implementations
impl Send for Trie
impl Unpin for Trie
impl Sync for Trie
impl UnwindSafe for Trie
impl RefUnwindSafe for Trie
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,