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 + } +}