From 6cc1df1b1e9af4ab5c3df7a10ab92c3e34d65e9e Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Thu, 17 Oct 2024 20:31:32 +0700 Subject: [PATCH 1/5] ref: refactor anagram --- src/string/anagram.rs | 108 +++++++++++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/src/string/anagram.rs b/src/string/anagram.rs index b81b7804707..41684e282e5 100644 --- a/src/string/anagram.rs +++ b/src/string/anagram.rs @@ -1,27 +1,101 @@ -pub fn check_anagram(s: &str, t: &str) -> bool { - sort_string(s) == sort_string(t) +use std::collections::HashMap; + +/// Custom error type representing an invalid character found in the input. +#[derive(Debug, PartialEq)] +pub enum AnagramError { + NonAlphabeticCharacter, } -fn sort_string(s: &str) -> Vec { - let mut res: Vec = s.to_ascii_lowercase().chars().collect::>(); - res.sort_unstable(); - res +/// Checks if two strings are anagrams, ignoring spaces and case sensitivity. +/// +/// # Arguments +/// +/// * `s` - First input string. +/// * `t` - Second input string. +/// +/// # Returns +/// +/// * `Ok(true)` if the strings are anagrams. +/// * `Ok(false)` if the strings are not anagrams. +/// * `Err(AnagramError)` if either string contains non-alphabetic characters. +pub fn check_anagram(s: &str, t: &str) -> Result { + let s_cleaned = clean_string(s)?; + let t_cleaned = clean_string(t)?; + + Ok(char_frequency(&s_cleaned) == char_frequency(&t_cleaned)) +} + +/// Cleans the input string by removing spaces and converting to lowercase. +/// Returns an error if any non-alphabetic character is found. +/// +/// # Arguments +/// +/// * `s` - Input string to clean. +/// +/// # Returns +/// +/// * `Ok(String)` containing the cleaned string (no spaces, lowercase). +/// * `Err(AnagramError)` if the string contains non-alphabetic characters. +fn clean_string(s: &str) -> Result { + s.chars() + .filter(|c| !c.is_whitespace()) + .map(|c| { + if c.is_alphabetic() { + Ok(c.to_ascii_lowercase()) + } else { + Err(AnagramError::NonAlphabeticCharacter) + } + }) + .collect() +} + +/// Computes the frequency of characters in a string. +/// +/// # Arguments +/// +/// * `s` - Input string. +/// +/// # Returns +/// +/// * A `HashMap` where the keys are characters and values are their frequencies. +fn char_frequency(s: &str) -> HashMap { + let mut freq = HashMap::new(); + for c in s.chars() { + *freq.entry(c).or_insert(0) += 1; + } + freq } #[cfg(test)] mod tests { use super::*; - #[test] - fn test_check_anagram() { - assert!(check_anagram("", "")); - assert!(check_anagram("A", "a")); - assert!(check_anagram("anagram", "nagaram")); - assert!(check_anagram("abcde", "edcba")); - assert!(check_anagram("sIlEnT", "LiStEn")); - - assert!(!check_anagram("", "z")); - assert!(!check_anagram("a", "z")); - assert!(!check_anagram("rat", "car")); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (s, t, expected) = $test_case; + assert_eq!(check_anagram(s, t), expected); + assert_eq!(check_anagram(t, s), expected); + } + )* + } + } + + test_cases! { + empty_strings: ("", "", Ok(true)), + empty_and_non_empty: ("", "Ted Morgan", Ok(false)), + single_char_same: ("z", "Z", Ok(true)), + single_char_diff: ("g", "h", Ok(false)), + valid_anagram_lowercase: ("cheater", "teacher", Ok(true)), + valid_anagram_with_spaces: ("Madam Curie", "Radium came", Ok(true)), + valid_anagram_mixed_cases: ("Satan", "Santa", Ok(true)), + valid_anagram_awesome: ("Anna Madrigal", "A man and a girl", Ok(true)), + non_anagram: ("rat", "car", Ok(false)), + invalid_anagram_with_special_char: ("hello!", "world", Err(AnagramError::NonAlphabeticCharacter)), + invalid_anagram_with_numeric_chars: ("test123", "321test", Err(AnagramError::NonAlphabeticCharacter)), + invalid_anagram_with_symbols: ("check@anagram", "check@nagaram", Err(AnagramError::NonAlphabeticCharacter)), + non_anagram_length_mismatch: ("abc", "abcd", Ok(false)), } } From cff0c5df7f53aecbe5983d98ff1ed5369a472fb5 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Fri, 25 Oct 2024 22:10:04 +0700 Subject: [PATCH 2/5] chore: rename `char_frequency` to `char_count` --- src/string/anagram.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/string/anagram.rs b/src/string/anagram.rs index 41684e282e5..5c030855e63 100644 --- a/src/string/anagram.rs +++ b/src/string/anagram.rs @@ -22,7 +22,7 @@ pub fn check_anagram(s: &str, t: &str) -> Result { let s_cleaned = clean_string(s)?; let t_cleaned = clean_string(t)?; - Ok(char_frequency(&s_cleaned) == char_frequency(&t_cleaned)) + Ok(char_count(&s_cleaned) == char_count(&t_cleaned)) } /// Cleans the input string by removing spaces and converting to lowercase. @@ -58,7 +58,7 @@ fn clean_string(s: &str) -> Result { /// # Returns /// /// * A `HashMap` where the keys are characters and values are their frequencies. -fn char_frequency(s: &str) -> HashMap { +fn char_count(s: &str) -> HashMap { let mut freq = HashMap::new(); for c in s.chars() { *freq.entry(c).or_insert(0) += 1; From 7ad2b6539502eac2c3ce1bbdcef849732b1e68a1 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Sat, 26 Oct 2024 11:00:09 +0700 Subject: [PATCH 3/5] tests: add some edge tests --- src/string/anagram.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/string/anagram.rs b/src/string/anagram.rs index 5c030855e63..71e1720abff 100644 --- a/src/string/anagram.rs +++ b/src/string/anagram.rs @@ -89,9 +89,19 @@ mod tests { single_char_same: ("z", "Z", Ok(true)), single_char_diff: ("g", "h", Ok(false)), valid_anagram_lowercase: ("cheater", "teacher", Ok(true)), - valid_anagram_with_spaces: ("Madam Curie", "Radium came", Ok(true)), + valid_anagram_with_spaces: ("madam curie", "radium came", Ok(true)), valid_anagram_mixed_cases: ("Satan", "Santa", Ok(true)), - valid_anagram_awesome: ("Anna Madrigal", "A man and a girl", Ok(true)), + valid_anagram_with_spaces_and_mixed_cases: ("Anna Madrigal", "A man and a girl", Ok(true)), + new_york_times: ("New York Times", "monkeys write", Ok(true)), + church_of_scientology: ("Church of Scientology", "rich chosen goofy cult", Ok(true)), + mcdonalds_restaurants: ("McDonald's restaurants", "Uncle Sam's standard rot", Err(AnagramError::NonAlphabeticCharacter)), + coronavirus: ("coronavirus", "carnivorous", Ok(true)), + synonym_evil: ("evil", "vile", Ok(true)), + synonym_gentleman: ("a gentleman", "elegant man", Ok(true)), + antigram: ("restful", "fluster", Ok(true)), + sentences: ("William Shakespeare", "I am a weakish speller", Ok(true)), + part_of_speech_adj_to_verb: ("silent", "listen", Ok(true)), + anagrammatized: ("Anagrams", "Ars magna", Ok(true)), non_anagram: ("rat", "car", Ok(false)), invalid_anagram_with_special_char: ("hello!", "world", Err(AnagramError::NonAlphabeticCharacter)), invalid_anagram_with_numeric_chars: ("test123", "321test", Err(AnagramError::NonAlphabeticCharacter)), From b589d2ac52bb40be9cd7fb795f1690ad7e074c82 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sat, 26 Oct 2024 15:58:52 +0200 Subject: [PATCH 4/5] style: rename local variable --- src/string/anagram.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/string/anagram.rs b/src/string/anagram.rs index 71e1720abff..0cb04548887 100644 --- a/src/string/anagram.rs +++ b/src/string/anagram.rs @@ -57,13 +57,13 @@ fn clean_string(s: &str) -> Result { /// /// # Returns /// -/// * A `HashMap` where the keys are characters and values are their frequencies. +/// * A `HashMap` where the keys are characters and values are their count. fn char_count(s: &str) -> HashMap { - let mut freq = HashMap::new(); + let mut res = HashMap::new(); for c in s.chars() { - *freq.entry(c).or_insert(0) += 1; + *res.entry(c).or_insert(0) += 1; } - freq + res } #[cfg(test)] From b23901c7e75d09ea7a93a3050cae77f84b871849 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Sat, 26 Oct 2024 15:59:58 +0200 Subject: [PATCH 5/5] docs: remove frequency from doc-str --- src/string/anagram.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/string/anagram.rs b/src/string/anagram.rs index 0cb04548887..9ea37dc4f6f 100644 --- a/src/string/anagram.rs +++ b/src/string/anagram.rs @@ -49,7 +49,7 @@ fn clean_string(s: &str) -> Result { .collect() } -/// Computes the frequency of characters in a string. +/// Computes the histogram of characters in a string. /// /// # Arguments ///