From f9c25aeec3eef84a22b67202e18650eef7cb1638 Mon Sep 17 00:00:00 2001 From: mengfansheng Date: Mon, 6 May 2024 17:12:57 +0800 Subject: [PATCH 1/2] check_permutation.rs --- DIRECTORY.md | 1 + src/string/check_permutation.rs | 40 +++++++++++++++++++++++++++++++++ src/string/mod.rs | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 src/string/check_permutation.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index d2b7f13b6d7..ad920c4113b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/src/string/check_permutation.rs b/src/string/check_permutation.rs new file mode 100644 index 00000000000..35e9dc6baea --- /dev/null +++ b/src/string/check_permutation.rs @@ -0,0 +1,40 @@ +//! Given two strings s1 and s2 consisting of lowercase letters. +//! +//! Determine whether the characters of one of the strings can be +//! rearranged to become another string. + +pub fn check_permutation(s1: &str, s2: &str) -> bool { + if s1.len() != s2.len() { + return false; + } + s1.chars() + .all(|c| s1.matches(c).count() == s2.matches(c).count()) +} + +#[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), + not_permutation: ("abc", "bab", false), + is_permutation_unicode: ("常威打来福", "来福打常威", true), + not_permutation_unicode: ("常威打来福", "来福骂常威", false), + empty: ("", "", true), + different_length: ("abc", "abcd", false), + } +} diff --git a/src/string/mod.rs b/src/string/mod.rs index e3a8ef3761c..b22a2a1ae53 100644 --- a/src/string/mod.rs +++ b/src/string/mod.rs @@ -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; @@ -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; From ac3338c9df9d60d7a9bdb4f346cc3048798cddbf Mon Sep 17 00:00:00 2001 From: mengfansheng Date: Tue, 7 May 2024 09:49:16 +0800 Subject: [PATCH 2/2] optimization algorithm --- src/string/check_permutation.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/string/check_permutation.rs b/src/string/check_permutation.rs index 35e9dc6baea..952da7b3fdf 100644 --- a/src/string/check_permutation.rs +++ b/src/string/check_permutation.rs @@ -1,14 +1,32 @@ -//! Given two strings s1 and s2 consisting of lowercase letters. +//! 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; } - s1.chars() - .all(|c| s1.matches(c).count() == s2.matches(c).count()) + + 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)] @@ -31,6 +49,7 @@ mod tests { 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),