Interview question: Length of Last Word
Given a string s consisting of only printable ASCII characters, write a function length_of_last_word(s: str) -> int
that returns the length of the last word in the string.
A word is defined as a maximal substring consisting of non-space characters only.
If there are no words in the string, return 0.
Note that the input string may contain leading or trailing spaces, and the words are separated by one or more spaces.
Example 1
Input: s = "Hello World"
Result: 5
Example 2
Input: s = " "
Result: 0
Example 3
Input: s = "a "
Result: 1