|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +struct DictionaryNode { |
| 4 | + value: char, |
| 5 | + items: HashMap<char, DictionaryNode>, |
| 6 | + ends_at: bool, |
| 7 | +} |
| 8 | + |
| 9 | +impl DictionaryNode { |
| 10 | + |
| 11 | + pub fn new(value: char) -> Self { |
| 12 | + Self { |
| 13 | + value, |
| 14 | + items: HashMap::new(), |
| 15 | + ends_at: false, |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | +} |
| 20 | + |
| 21 | +struct Dictionary { |
| 22 | + root: DictionaryNode |
| 23 | +} |
| 24 | + |
| 25 | +impl Dictionary { |
| 26 | + |
| 27 | + pub fn new() -> Self { |
| 28 | + Self { |
| 29 | + root: DictionaryNode::new('*') |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn add_word(&mut self, word: String) { |
| 34 | + let mut current = &mut self.root; |
| 35 | + for letter in word.chars() { |
| 36 | + if !current.items.contains_key(&letter) { |
| 37 | + let node = DictionaryNode::new(letter); |
| 38 | + current.items.insert(letter, node); |
| 39 | + } |
| 40 | + current = current.items.get_mut(&letter).unwrap(); |
| 41 | + } |
| 42 | + current.ends_at = true; |
| 43 | + } |
| 44 | + |
| 45 | + pub fn search( |
| 46 | + &mut self, |
| 47 | + results: &mut Vec<Vec<i32>>, |
| 48 | + letters: &Vec<char>, |
| 49 | + start: usize |
| 50 | + ) { |
| 51 | + let n = letters.len(); |
| 52 | + let mut current = &self.root; |
| 53 | + |
| 54 | + for i in start..n { |
| 55 | + let letter = letters[i]; |
| 56 | + if current.items.contains_key(&letter) { |
| 57 | + current = ¤t.items[&letter]; |
| 58 | + } else { |
| 59 | + break; |
| 60 | + } |
| 61 | + if current.ends_at { |
| 62 | + results.push(vec![start as i32, i as i32]); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | + |
| 69 | +/// Given a string `text` and an array of strings `words`, return an array of all index pairs `[i, |
| 70 | +/// j]` so that the substring `text[i...j]` is in `words`. |
| 71 | +/// |
| 72 | +/// Return the pairs `[i,j]` in sorted order (i.e., sort them by their first coordinate, and in |
| 73 | +/// case of ties sort them by their second coordinate). |
| 74 | +struct Solution; |
| 75 | + |
| 76 | +impl Solution { |
| 77 | + |
| 78 | + fn to_dictionary(words: Vec<String>) -> Dictionary { |
| 79 | + let mut result = Dictionary::new(); |
| 80 | + for word in words { |
| 81 | + result.add_word(word); |
| 82 | + } |
| 83 | + result |
| 84 | + } |
| 85 | + |
| 86 | + pub fn index_pairs(text: String, words: Vec<String>) -> Vec<Vec<i32>> { |
| 87 | + let mut results: Vec<Vec<i32>> = Vec::new(); |
| 88 | + |
| 89 | + let mut dict = Self::to_dictionary(words); |
| 90 | + let letters: Vec<char> = text.chars().collect(); |
| 91 | + let n = letters.len(); |
| 92 | + |
| 93 | + for i in 0..n { |
| 94 | + dict.search(&mut results, &letters, i); |
| 95 | + } |
| 96 | + |
| 97 | + results |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::Solution; |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn example_1() { |
| 108 | + let text = str!("thestoryofleetcodeandme"); |
| 109 | + let words = vec![str!("story"), str!("fleet"), str!("leetcode")]; |
| 110 | + let result = Solution::index_pairs(text, words); |
| 111 | + assert_eq!(result, vec![vec![3,7], vec![9,13], vec![10,17]]); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn example_2() { |
| 116 | + let text = str!("ababa"); |
| 117 | + let words = vec![str!("aba"), str!("ab")]; |
| 118 | + let result = Solution::index_pairs(text, words); |
| 119 | + assert_eq!(result, vec![vec![0,1], vec![0,2], vec![2,3], vec![2,4]]); |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments