id 1406159642 Time: 1 ms MemUsage: 2.3 MB
This commit is contained in:
28
leetcode/isomorphic-strings/solution_1406159642.rs
Normal file
28
leetcode/isomorphic-strings/solution_1406159642.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
impl Solution {
|
||||
pub fn is_isomorphic(s: String, t: String) -> bool {
|
||||
let mut charmap = HashMap::new();
|
||||
let mut charset = HashSet::new();
|
||||
for (sc, tc) in s.chars().zip(t.chars()){
|
||||
match charmap.get(&sc){
|
||||
Some(&msc) => {
|
||||
if msc != tc {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
None => {
|
||||
if charset.contains(&tc){
|
||||
return false;
|
||||
}
|
||||
charset.insert(tc);
|
||||
charmap.insert(sc, tc);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user