Skip to content

Commit

Permalink
Add problem 0520: Detect Capital
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Jun 13, 2024
1 parent f1934f5 commit ef644be
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions src/problem_0520_detect_capital/iterative.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}
36 changes: 36 additions & 0 deletions src/problem_0520_detect_capital/mod.rs
Original file line number Diff line number Diff line change
@@ -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<S: Solution>() {
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);
}
}
}
26 changes: 26 additions & 0 deletions src/problem_0520_detect_capital/pattern_matching.rs
Original file line number Diff line number Diff line change
@@ -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::<super::Solution>();
}
}

0 comments on commit ef644be

Please sign in to comment.