From c505df78fa9c03c81ba7760484083ede31075440 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 5 Apr 2024 23:46:19 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB205.=20Isomorphic=20Strings?= =?UTF-8?q?=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/isomorphic-strings.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 rs/isomorphic-strings.rs diff --git a/rs/isomorphic-strings.rs b/rs/isomorphic-strings.rs new file mode 100644 index 0000000..0eeebe6 --- /dev/null +++ b/rs/isomorphic-strings.rs @@ -0,0 +1,27 @@ +use std::collections::{HashMap, HashSet}; + +impl Solution { + pub fn is_isomorphic(s: String, t: String) -> bool { + let mut projection: HashMap = HashMap::new(); + let mut used: HashSet = HashSet::new(); + + for (l, r) in s.chars().zip(t.chars()) { + match projection.get(&l) { + Some(&expected_r) if expected_r != r => { + return false; + } + None => { + if used.contains(&r) { + return false; + } + + projection.insert(l, r); + used.insert(r); + } + _ => { /* no-op */ } + } + } + + true + } +}