-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0205_isomorphic_strings.rs
More file actions
42 lines (35 loc) · 937 Bytes
/
s0205_isomorphic_strings.rs
File metadata and controls
42 lines (35 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#![allow(unused)]
pub struct Solution {}
impl Solution {
// O(N) O(N)
pub fn is_isomorphic(s: String, t: String) -> bool {
use std::collections::HashMap;
let mut m1 = HashMap::<char, usize>::new();
let mut m2 = HashMap::<char, usize>::new();
for (i, (ch1, ch2)) in s.chars().zip(t.chars()).enumerate() {
if m1.insert(ch1, i) != m2.insert(ch2, i) {
return false;
}
}
true
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_205() {
assert_eq!(
Solution::is_isomorphic("egg".to_string(), "add".to_string()),
true
);
assert_eq!(
Solution::is_isomorphic("foo".to_string(), "bar".to_string()),
false
);
assert_eq!(
Solution::is_isomorphic("paper".to_string(), "title".to_string()),
true
);
}
}