20240311 Finished

This commit is contained in:
jackfiled 2024-03-11 11:13:02 +08:00
parent f6b72beab9
commit 590081ab0a
2 changed files with 39 additions and 1 deletions

View File

@ -70,3 +70,4 @@ mod p2575_find_the_divisibility_array_of_a_string;
mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array; mod p2834_find_the_minimum_possible_sum_of_a_beautiful_array;
mod p2386_find_the_k_sum_of_an_array; mod p2386_find_the_k_sum_of_an_array;
mod p299_bulls_and_cows; mod p299_bulls_and_cows;
mod p2129_capitalize_the_title;

View File

@ -0,0 +1,37 @@
/**
* [2129] Capitalize the Title
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn capitalize_title(title: String) -> String {
let mut words: Vec<&str> = title.split(' ').collect();
let mut result = String::new();
for word in words.iter_mut() {
if word.len() < 3 {
result = result + &word.to_lowercase() + " ";
} else {
result = result + &word[0..1].to_uppercase() + &word[1..].to_lowercase() + " ";
}
}
result.trim().to_owned()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2129() {
assert_eq!("Capitalize The Title", Solution::capitalize_title("capiTalIze tHe titLe".to_owned()));
assert_eq!("First Letter of Each Word", Solution::capitalize_title("First leTTeR of EACH Word".to_owned()));
}
}