mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
27 lines
747 B
Rust
27 lines
747 B
Rust
use std::collections::{HashMap, HashSet};
|
|
|
|
impl Solution {
|
|
pub fn is_isomorphic(s: String, t: String) -> bool {
|
|
let mut projection: HashMap<char, char> = HashMap::new();
|
|
let mut used: HashSet<char> = 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
|
|
}
|
|
}
|