Skip to content
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

add check_permutation.rs #711

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
* [Autocomplete Using Trie](https://github.com/TheAlgorithms/Rust/blob/master/src/string/autocomplete_using_trie.rs)
* [Boyer Moore Search](https://github.com/TheAlgorithms/Rust/blob/master/src/string/boyer_moore_search.rs)
* [Burrows Wheeler Transform](https://github.com/TheAlgorithms/Rust/blob/master/src/string/burrows_wheeler_transform.rs)
* [Check Permutation](https://github.com/TheAlgorithms/Rust/blob/master/src/string/check_permutation.rs)
* [Duval Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/string/duval_algorithm.rs)
* [Hamming Distance](https://github.com/TheAlgorithms/Rust/blob/master/src/string/hamming_distance.rs)
* [Isomorphism](https://github.com/TheAlgorithms/Rust/blob/master/src/string/isomorphism.rs)
Expand Down
59 changes: 59 additions & 0 deletions src/string/check_permutation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//! Given two strings s1 and s2.
//!
//! Determine whether the characters of one of the strings can be
//! rearranged to become another string.
use std::collections::HashMap;

pub fn check_permutation(s1: &str, s2: &str) -> bool {
if s1.len() != s2.len() {
return false;
}

let mut map = HashMap::new();
// Record the number of occurrences of the s1 character
for c in s1.chars() {
let count = map.entry(c).or_insert(0);
*count += 1;
}
// Iterate through s2 , if encountered the number of occurrences of 0
// indicates the existence of s1 does not exist in the character.
for c in s2.chars() {
let count = map.entry(c).or_insert(0);
if *count == 0 {
return false;
} else {
*count -= 1;
}
}

true
}

#[cfg(test)]
mod tests {
macro_rules! test_check_permutation {
($($name:ident: $inputs:expr,)*) => {
$(
#[test]
fn $name() {
use super::check_permutation;
let (s, t, expected) = $inputs;
assert_eq!(check_permutation(s, t), expected);
assert_eq!(check_permutation(t, s), expected);
assert!(check_permutation(s, s));
assert!(check_permutation(t, t));
}
)*
}
}

test_check_permutation! {
is_permutation: ("abc", "bca", true),
is_permutation1: ("aBc", "Bca", true),
not_permutation: ("abc", "bab", false),
is_permutation_unicode: ("常威打来福", "来福打常威", true),
not_permutation_unicode: ("常威打来福", "来福骂常威", false),
empty: ("", "", true),
different_length: ("abc", "abcd", false),
}
}
2 changes: 2 additions & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod anagram;
mod autocomplete_using_trie;
mod boyer_moore_search;
mod burrows_wheeler_transform;
mod check_permutation;
mod duval_algorithm;
mod hamming_distance;
mod isomorphism;
Expand All @@ -29,6 +30,7 @@ pub use self::boyer_moore_search::boyer_moore_search;
pub use self::burrows_wheeler_transform::{
burrows_wheeler_transform, inv_burrows_wheeler_transform,
};
pub use self::check_permutation::check_permutation;
pub use self::duval_algorithm::duval_algorithm;
pub use self::hamming_distance::hamming_distance;
pub use self::isomorphism::is_isomorphic;
Expand Down