-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0288_unique_word_abbreviation.rs
More file actions
65 lines (54 loc) · 1.51 KB
/
s0288_unique_word_abbreviation.rs
File metadata and controls
65 lines (54 loc) · 1.51 KB
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
#![allow(unused)]
use std::collections::{HashMap, HashSet};
struct ValidWordAbbr {
abbr_dict: HashMap<String, bool>,
dict: HashSet<String>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl ValidWordAbbr {
fn new(dictionary: Vec<String>) -> Self {
let mut abbr_dict = HashMap::new();
for s in dictionary.iter() {
let abbr = Self::to_abbr(s);
let b = abbr_dict.contains_key(&abbr);
abbr_dict.insert(abbr, !b);
}
let dict = dictionary.into_iter().collect::<HashSet<String>>();
Self {
abbr_dict: abbr_dict,
dict: dict,
}
}
fn to_abbr(s: &String) -> String {
let mut ret = String::from("");
let n = s.len();
if n <= 2 {
return s.clone();
}
ret.push(s.chars().nth(0).unwrap());
ret.push_str((n - 2).to_string().as_str());
ret.push(s.chars().nth(n - 1).unwrap());
ret
}
fn is_unique(&self, word: String) -> bool {
let abbr = Self::to_abbr(&word);
match self.abbr_dict.get(&abbr) {
None => true,
Some(&a) => a && self.dict.contains(&word),
}
}
}
/**
* Your ValidWordAbbr object will be instantiated and called as such:
* let obj = ValidWordAbbr::new(dictionary);
* let ret_1: bool = obj.is_unique(word);
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_288() {}
}