LeetCode 290. Word Pattern

Problem Statement


use std::collections::HashMap;

impl Solution {
pub fn word_pattern(pattern: String, str: String) -> bool {
let inputs = str.split_whitespace().collect::<Vec<&str>>();
if pattern.len() != inputs.len() {
return false;
}
let (mut m0, mut m1) = (HashMap::new(), HashMap::new());
for i in 0..pattern.len() {
let char = pattern.chars().nth(i).unwrap();
if !m0.contains_key(&char) {
m0.insert(char, inputs[i].clone());
} else {
if *m0.get(&char).unwrap() != inputs[i] {
return false;
}
}

if !m1.contains_key(&inputs[i]) {
m1.insert(inputs[i].clone(), char);
} else {
if *m1.get(&inputs[i]).unwrap() != char {
return false;
}
}
}

return true;
}
}