Last active
October 18, 2023 10:04
-
-
Save frectonz/275e967a7900d628888f51dbde11163a to your computer and use it in GitHub Desktop.
From cassidoo's October 18, 2023 newsletter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn is_isomorphic(s1: &str, s2: &str) -> bool { | |
let mask1 = str_mask(s1); | |
let mask2 = str_mask(s2); | |
mask1 == mask2 | |
} | |
fn str_mask(s: &str) -> Vec<usize> { | |
s.chars() | |
.fold((None, Vec::<usize>::new()), |(last_c, mut acc), curr_c| { | |
if last_c == Some(curr_c) { | |
let last = acc.pop().map(|e| e + 1).unwrap_or(1); | |
acc.push(last); | |
} else { | |
acc.push(1); | |
} | |
(Some(curr_c), acc) | |
}) | |
.1 | |
} | |
fn main() { | |
assert!(is_isomorphic("abb", "cdd")); | |
assert!(!is_isomorphic("cassidy", "1234567")); | |
assert!(is_isomorphic("cass", "1233")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment