-
Notifications
You must be signed in to change notification settings - Fork 2
/
implement-magic-dictionary.rs
95 lines (78 loc) · 2.12 KB
/
implement-magic-dictionary.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
/**
* Your MagicDictionary object will be instantiated and called as such:
* let obj = MagicDictionary::new();
* obj.build_dict(dictionary);
* let ret_2: bool = obj.search(searchWord);
*/
#[derive(Clone)]
struct Tier {
data: Vec<Option<Box<Tier>>>,
is_end: bool,
}
impl Tier {
fn new() -> Self {
Tier {
data: vec![None; 26],
is_end: false,
}
}
fn insert(&mut self, data: &[u8]) {
if data.is_empty() {
return;
}
let mut node = self;
for i in data {
let index = (i - b'a') as usize;
if node.data[index].is_none() {
node.data[index] = Some(Box::new(Tier::new()));
}
node = node.data[index].as_mut().unwrap();
}
node.is_end = true;
}
/// flag为true表示已经变换过
fn search(&self, data: &[u8], flag: bool) -> bool {
if data.is_empty() {
return self.is_end && flag;
}
let index = (data[0] - b'a') as usize;
if let Some(x) = self.data[index].as_ref() {
if x.search(&data[1..], flag | false) {
return true;
}
}
if !flag {
for i in (0..26).filter(|&x| x != index) {
if let Some(x) = self.data[i].as_ref() {
if x.search(&data[1..], true) {
return true;
}
}
}
}
false
}
}
struct MagicDictionary {
tier: Tier,
}
impl MagicDictionary {
fn new() -> Self {
Self { tier: Tier::new() }
}
fn build_dict(&mut self, dictionary: Vec<String>) {
dictionary
.into_iter()
.for_each(|x| self.tier.insert(x.as_bytes()));
}
fn search(&self, search_word: String) -> bool {
self.tier.search(search_word.as_bytes(), false)
}
}