Skip to content

Commit

Permalink
Add problem 2309: Greatest English Letter in Upper and Lower Case
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 6, 2024
1 parent 774316f commit c9f0085
Show file tree
Hide file tree
Showing 3 changed files with 50 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 @@ -1714,6 +1714,7 @@ pub mod problem_2300_successful_pairs_of_spells_and_potions;
pub mod problem_2302_count_subarrays_with_score_less_than_k;
pub mod problem_2303_calculate_amount_paid_in_taxes;
pub mod problem_2305_fair_distribution_of_cookies;
pub mod problem_2309_greatest_english_letter_in_upper_and_lower_case;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

use std::num::NonZeroU64;

impl Solution {
pub fn greatest_letter(s: String) -> String {
let state = s.bytes().fold(0, |state, c| state | 1 << (c - b'A'));

NonZeroU64::new(state & (state >> (b'a' - b'A'))).map_or(String::new(), |state| {
char::from(b'A' + 63 - state.leading_zeros() as u8).to_string()
})
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn greatest_letter(s: String) -> String {
Self::greatest_letter(s)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn greatest_letter(s: String) -> String;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [("lEeTcOdE", "E"), ("arRAzFif", "R"), ("AbCdEfGhIjK", "")];

for (s, expected) in test_cases {
assert_eq!(S::greatest_letter(s.to_string()), expected);
}
}
}

0 comments on commit c9f0085

Please sign in to comment.