1
Fork 0

Solve length-of-last-word.

This commit is contained in:
Bauke 2022-04-07 19:33:45 +02:00
parent 83519fe16d
commit 99077283ba
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 18 additions and 0 deletions

View File

@ -0,0 +1,17 @@
pub fn length_of_last_word(string: String) -> i32 {
string
.split_whitespace()
.last()
.map(|word| word.len())
.unwrap_or_default() as i32
}
#[test]
fn test_length_of_last_word() {
assert_eq!(length_of_last_word("Hello World".to_string()), 5);
assert_eq!(
length_of_last_word(" fly me to the moon ".to_string()),
4
);
assert_eq!(length_of_last_word("luffy is still joyboy".to_string()), 6);
}

View File

@ -1,4 +1,5 @@
pub mod implement_strstr; pub mod implement_strstr;
pub mod length_of_last_word;
pub mod longest_common_prefix; pub mod longest_common_prefix;
pub mod palindrome_number; pub mod palindrome_number;
pub mod plus_one; pub mod plus_one;