-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add isomorphism.rs
#707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add isomorphism.rs
#707
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2458f07
isomorphic strings
changweidalaifu6 1b0eb45
modify the code as suggested
changweidalaifu6 7e03c4b
modify DIRECTORY.md
changweidalaifu6 2c8184b
modify docstring
changweidalaifu6 a47fb08
tests: add test cases with longer inputs
vil02 f4e46c0
Merge branch 'master' into master
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! This module provides functionality to determine whether two strings are isomorphic. | ||
//! | ||
//! Two strings are considered isomorphic if the characters in one string can be replaced | ||
//! by some mapping relation to obtain the other string. | ||
use std::collections::HashMap; | ||
|
||
/// Determines whether two strings are isomorphic. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `s` - The first string. | ||
/// * `t` - The second string. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `true` if the strings are isomorphic, `false` otherwise. | ||
pub fn is_isomorphic(s: &str, t: &str) -> bool { | ||
let s_chars: Vec<char> = s.chars().collect(); | ||
let t_chars: Vec<char> = t.chars().collect(); | ||
if s_chars.len() != t_chars.len() { | ||
return false; | ||
} | ||
let mut s_to_t_map = HashMap::new(); | ||
let mut t_to_s_map = HashMap::new(); | ||
for (s_char, t_char) in s_chars.into_iter().zip(t_chars) { | ||
if !check_mapping(&mut s_to_t_map, s_char, t_char) | ||
|| !check_mapping(&mut t_to_s_map, t_char, s_char) | ||
{ | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
/// Checks the mapping between two characters and updates the map. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `map` - The HashMap to store the mapping. | ||
/// * `key` - The key character. | ||
/// * `value` - The value character. | ||
/// | ||
/// # Returns | ||
/// | ||
/// `true` if the mapping is consistent, `false` otherwise. | ||
fn check_mapping(map: &mut HashMap<char, char>, key: char, value: char) -> bool { | ||
vil02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
match map.get(&key) { | ||
Some(&mapped_char) => mapped_char == value, | ||
None => { | ||
map.insert(key, value); | ||
true | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::is_isomorphic; | ||
macro_rules! test_is_isomorphic { | ||
($($name:ident: $inputs:expr,)*) => { | ||
$( | ||
#[test] | ||
fn $name() { | ||
let (s, t, expected) = $inputs; | ||
assert_eq!(is_isomorphic(s, t), expected); | ||
assert_eq!(is_isomorphic(t, s), expected); | ||
assert!(is_isomorphic(s, s)); | ||
assert!(is_isomorphic(t, t)); | ||
} | ||
)* | ||
} | ||
} | ||
test_is_isomorphic! { | ||
isomorphic: ("egg", "add", true), | ||
isomorphic_long: ("abcdaabdcdbbabababacdadad", "AbCdAAbdCdbbAbAbAbACdAdAd", true), | ||
not_isomorphic: ("egg", "adc", false), | ||
non_isomorphic_long: ("abcdaabdcdbbabababacdadad", "AACdAAbdCdbbAbAbAbACdAdAd", false), | ||
isomorphic_unicode: ("天苍苍", "野茫茫", true), | ||
isomorphic_unicode_different_byte_size: ("abb", "野茫茫", true), | ||
empty: ("", "", true), | ||
different_length: ("abc", "abcd", false), | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.