-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword-pattern.rs
37 lines (29 loc) · 904 Bytes
/
word-pattern.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
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
impl Solution {
pub fn word_pattern(pattern: String, s: String) -> bool {
let mut m = std::collections::HashMap::<u8, &str>::new();
let mut m1 = std::collections::HashMap::<&str, u8>::new();
let s = s.split(' ').collect::<Vec<&str>>();
let pattern = pattern.as_bytes();
if s.len() != pattern.len() {
return false;
}
for i in 0..s.len() {
if let Some(&v) = m1.get(&s[i]) {
if v != pattern[i] {
return false;
}
}
if let Some(&v) = m.get(&pattern[i]) {
if v != s[i] {
return false;
}
}
m1.insert(s[i], pattern[i]);
m.insert(pattern[i], s[i]);
}
true
}
}