Skip to content

Commit

Permalink
leetcode: Add 2309
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 15, 2024
1 parent 7aa18f4 commit b173d7f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "lc-2309-greatest-english-letter-in-upper-and-lower-case"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use std::collections::HashSet;

// HashTable
pub fn greatest_letter1(s: String) -> String {
debug_assert!(!s.is_empty());

// 构造字符集
let set: HashSet<char> = s.chars().collect();

let mut largest_letter: char = ' ';

// 遍历字符串所有的字符
for chr in s.chars() {
if (chr.is_ascii_lowercase() && set.contains(&chr.to_ascii_uppercase()))
|| (chr.is_ascii_uppercase() && set.contains(&chr.to_ascii_lowercase()))
{
largest_letter = largest_letter.max(chr);
}
}
if largest_letter == ' ' {
String::new()
} else {
largest_letter.to_ascii_uppercase().into()
}
}

pub type SolutionFn = fn(String) -> String;

fn check_solution(func: SolutionFn) {
let s = "lEeTcOdE".to_owned();
assert_eq!(func(s), "E".to_owned());

let s = "arRAzFif".to_owned();
assert_eq!(func(s), "R".to_owned());

let s = "AbCdEfGhIjK".to_owned();
assert_eq!(func(s), "".to_owned());
}

fn main() {
check_solution(greatest_letter1);
}

#[cfg(test)]
mod tests {
use super::{check_solution, greatest_letter1};

#[test]
fn test_greatest_letter1() {
check_solution(greatest_letter1);
}
}

0 comments on commit b173d7f

Please sign in to comment.