Skip to content

Commit

Permalink
70: Submit
Browse files Browse the repository at this point in the history
  • Loading branch information
sankichi92 committed Dec 31, 2023
1 parent 6092273 commit 0b95560
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions 70-implement-trie-prefix-tree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,42 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
use std::collections::HashSet;

pub struct Trie {
words: HashSet<String>,
}

impl Trie {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Trie {
words: HashSet::new(),
}
}

pub fn insert(&mut self, word: String) {
self.words.insert(word);
}

pub fn search(&self, word: String) -> bool {
self.words.contains(&word)
}

pub fn starts_with(&self, prefix: String) -> bool {
self.words.iter().any(|word| word.starts_with(&prefix))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
fn case1() {
let mut trie = Trie::new();
trie.insert("apple".to_string());
assert!(trie.search("apple".to_string()));
assert!(!trie.search("app".to_string()));
assert!(trie.starts_with("app".to_string()));
trie.insert("app".to_string());
assert!(trie.search("app".to_string()));
}
}

0 comments on commit 0b95560

Please sign in to comment.