blob: 6161b40a69c8d4bcf457fce4bdf87c3bdf7bf7ff (
plain)
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
|
struct Solution;
impl Solution {
pub fn word_pattern(pattern: String, s: String) -> bool {
use std::collections::{HashMap, HashSet};
if pattern.len() != s.matches(' ').count() + 1 {
return false;
}
let (mut hm, mut word_set) = (HashMap::new(), HashSet::new());
for (word, c) in s.split_ascii_whitespace().zip(pattern.chars()) {
if let Some(w) = hm.insert(c, word) {
if w != word {
return false;
}
} else if !word_set.insert(word) {
return false;
}
}
true
}
}
fn main() {
let (pattern, s) = (String::from("abba"), String::from("dog cat cat dog"));
print!("{}", Solution::word_pattern(pattern, s));
}
|