-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/leetcode/2309.greatest-english-letter-in-upper-and-lower-case/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
4 changes: 4 additions & 0 deletions
4
src/leetcode/2309.greatest-english-letter-in-upper-and-lower-case/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# | ||
|
||
[问题描述](../problems/) |
56 changes: 56 additions & 0 deletions
56
src/leetcode/2309.greatest-english-letter-in-upper-and-lower-case/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |