-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboard-row.rs
68 lines (61 loc) · 1.67 KB
/
keyboard-row.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
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(
vec!["Alaska".to_string(), "Dad".to_string()],
Solution::find_words(vec![
"Hello".to_string(),
"Alaska".to_string(),
"Dad".to_string(),
"Peace".to_string()
])
);
}
struct Solution;
impl Solution {
pub fn find_words(words: Vec<String>) -> Vec<String> {
let v = vec![(); 10];
let m1: std::collections::HashMap<_, _> = "qwertyuiop"
.as_bytes()
.into_iter()
.map(|x| *x)
.zip(v.iter())
.collect();
let m2: std::collections::HashMap<_, _> = "asdfghjkl"
.as_bytes()
.into_iter()
.map(|x| *x)
.zip(v.iter())
.collect();
let m3: std::collections::HashMap<_, _> = "zxcvbnm"
.as_bytes()
.into_iter()
.map(|x| *x)
.zip(v.iter())
.collect();
let mut result = vec![];
for i in words.iter() {
let mut s = i.as_bytes()[0];
if s < 97 {
s += 32;
}
let m = if let Some(_) = m1.get(&s) {
&m1
} else if let Some(_) = m2.get(&s) {
&m2
} else {
&m3
};
let mut flag = true;
for j in i.as_bytes() {
if let (None, None) = (m.get(j), m.get(&(*j + 32))) {
flag = false;
break;
}
}
if flag {
result.push(i.clone())
}
}
result
}
}