#58
Easy Algorithms Length of last word
String
58.3% acceptance
Jan 12, 2026
6361
357
Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn length_of_last_word(s: String) -> i32 {
s.trim_end().split_whitespace().last().map_or(0, |word| word.len() as i32)
}
}