JuniorString
Length of Last Word
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. The string is guaranteed to contain at least one word.
Example 1
Input
s = "Hello World"Output
5The last word is
World, which has length 5.Example 2
Input
s = " fly me to the moon "Output
4After ignoring trailing spaces, the last word is
moon, which has length 4.Constraints
- 1 <= s.length <= 10^4
- s consists of only English letters and spaces ' '
- There will be at least one word in s