diff --git a/rust/0205-isomorphic-strings.rs b/rust/0205-isomorphic-strings.rs index fd8382435..6f07885be 100644 --- a/rust/0205-isomorphic-strings.rs +++ b/rust/0205-isomorphic-strings.rs @@ -5,14 +5,17 @@ impl Solution { let (mut map_s_to_t, mut map_t_to_s) = (HashMap::new(), HashMap::new()); for (c1, c2) in s.chars().zip(t.chars()) { - if (map_s_to_t.contains_key(&c1) && map_s_to_t.get(&c1) != Some(&c2)) - || (map_t_to_s.contains_key(&c2) && map_t_to_s.get(&c2) != Some(&c1)) - { - return false; + if let Some(w) = map_s_to_t.insert(c1, c2) { + if w != c2 { + return false; + } } - map_s_to_t.insert(c1, c2); - map_t_to_s.insert(c2, c1); + if let Some(w) = map_t_to_s.insert(c2, c1) { + if w != c1 { + return false; + } + } } true