@@ -22,12 +22,15 @@ impl Default for Node {
2222/// use programming_team_code_rust::data_structures::trie::Trie;
2323///
2424/// let mut trie = Trie::default();
25- /// trie.insert("HELLO");
26- /// trie.insert("HELLO");
27- /// trie.insert("WORLD");
28- /// assert_eq!(trie.find("HELLO"), 2);
29- /// assert_eq!(trie.find("WORLD"), 1);
30- /// assert_eq!(trie.find("WORLDS"), 0);
25+ /// let hello = vec!['H', 'E', 'L', 'L', 'O'];
26+ /// let world = vec!['W', 'O', 'R', 'L', 'D'];
27+ /// let hello_world = vec!['H', 'E', 'L', 'L', 'O', 'W', 'O', 'R', 'L', 'D'];
28+ /// trie.insert(&hello);
29+ /// trie.insert(&hello);
30+ /// trie.insert(&world);
31+ /// assert_eq!(trie.find(&hello), 2);
32+ /// assert_eq!(trie.find(&world), 1);
33+ /// assert_eq!(trie.find(&hello_world), 0);
3134/// ```
3235pub struct Trie {
3336 t : Vec < Node > ,
@@ -47,9 +50,9 @@ impl Trie {
4750 /// # Complexity
4851 /// - Time: O(|s|)
4952 /// - Space: O(|s|)
50- pub fn insert ( & mut self , s : & str ) {
53+ pub fn insert ( & mut self , s : & [ char ] ) {
5154 let mut v = 0 ;
52- for ch in s. chars ( ) {
55+ for & ch in s {
5356 let idx = ( ch as u8 - FIRST_CHAR as u8 ) as usize ;
5457 if self . t [ v] . next [ idx] . is_none ( ) {
5558 self . t [ v] . next [ idx] = Some ( self . t . len ( ) ) ;
@@ -65,9 +68,9 @@ impl Trie {
6568 /// # Complexity
6669 /// - Time: O(|s|)
6770 /// - Space: O(1)
68- pub fn find ( & self , s : & str ) -> usize {
71+ pub fn find ( & self , s : & [ char ] ) -> usize {
6972 let mut v = 0 ;
70- for ch in s. chars ( ) {
73+ for & ch in s {
7174 let idx = ( ch as u8 - FIRST_CHAR as u8 ) as usize ;
7275 if self . t [ v] . next [ idx] . is_none ( ) {
7376 return 0 ;
0 commit comments