From ef644be9f0e34714570b30c3dabc7d30305c1ec3 Mon Sep 17 00:00:00 2001 From: Spxg Date: Thu, 13 Jun 2024 20:58:49 +0800 Subject: [PATCH] Add problem 0520: Detect Capital --- src/lib.rs | 1 + src/problem_0520_detect_capital/iterative.rs | 33 +++++++++++++++++ src/problem_0520_detect_capital/mod.rs | 36 +++++++++++++++++++ .../pattern_matching.rs | 26 ++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 src/problem_0520_detect_capital/iterative.rs create mode 100644 src/problem_0520_detect_capital/mod.rs create mode 100644 src/problem_0520_detect_capital/pattern_matching.rs diff --git a/src/lib.rs b/src/lib.rs index 4ccf66e..c96b8aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -351,6 +351,7 @@ pub mod problem_0504_base_7; pub mod problem_0507_perfect_number; pub mod problem_0513_find_bottom_left_tree_value; pub mod problem_0515_find_largest_value_in_each_tree_row; +pub mod problem_0520_detect_capital; pub mod problem_0523_continuous_subarray_sum; pub mod problem_0525_contiguous_array; pub mod problem_0530_minimum_absolute_difference_in_bst; diff --git a/src/problem_0520_detect_capital/iterative.rs b/src/problem_0520_detect_capital/iterative.rs new file mode 100644 index 0000000..cf8ec1c --- /dev/null +++ b/src/problem_0520_detect_capital/iterative.rs @@ -0,0 +1,33 @@ +pub struct Solution; + +impl Solution { + pub fn detect_capital_use(word: String) -> bool { + let mut iter = word.chars(); + let first = iter.next().unwrap(); + if first.is_ascii_uppercase() { + iter.next().map_or(true, |n| { + if n.is_ascii_uppercase() { + iter.all(|x| x.is_ascii_uppercase()) + } else { + iter.all(|x| x.is_ascii_lowercase()) + } + }) + } else { + iter.all(|x| x.is_ascii_lowercase()) + } + } +} + +impl super::Solution for Solution { + fn detect_capital_use(word: String) -> bool { + Self::detect_capital_use(word) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0520_detect_capital/mod.rs b/src/problem_0520_detect_capital/mod.rs new file mode 100644 index 0000000..fef9e0d --- /dev/null +++ b/src/problem_0520_detect_capital/mod.rs @@ -0,0 +1,36 @@ +pub mod iterative; +pub mod pattern_matching; + +pub trait Solution { + fn detect_capital_use(word: String) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + ("USA", true), + ("FlaG", false), + ("U", true), + ("u", true), + ("US", true), + ("Us", true), + ("uS", false), + ("us", true), + ("USB", true), + ("USb", false), + ("UsB", false), + ("Usb", true), + ("uSB", false), + ("uSb", false), + ("usB", false), + ("usb", true), + ]; + + for (word, expected) in test_cases { + assert_eq!(S::detect_capital_use(word.to_string()), expected); + } + } +} diff --git a/src/problem_0520_detect_capital/pattern_matching.rs b/src/problem_0520_detect_capital/pattern_matching.rs new file mode 100644 index 0000000..f980747 --- /dev/null +++ b/src/problem_0520_detect_capital/pattern_matching.rs @@ -0,0 +1,26 @@ +pub struct Solution; + +impl Solution { + pub fn detect_capital_use(word: String) -> bool { + match word.as_bytes() { + [_] => true, + [b'A'..=b'Z', b'A'..=b'Z', rest @ ..] => rest.iter().all(u8::is_ascii_uppercase), + [b'A'..=b'Z', b'a'..=b'z', rest @ ..] => rest.iter().all(u8::is_ascii_lowercase), + word => word.iter().all(u8::is_ascii_lowercase), + } + } +} + +impl super::Solution for Solution { + fn detect_capital_use(word: String) -> bool { + Self::detect_capital_use(word) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}