rs: add «205. Isomorphic Strings»
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
04ecc74e91
commit
c505df78fa
1 changed files with 27 additions and 0 deletions
27
rs/isomorphic-strings.rs
Normal file
27
rs/isomorphic-strings.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue