Length of Last Word
Easy · strings
You are given a string s made of letters and spaces. A word is a maximal run of letters, so words are separated by one or more spaces, and the string may also start or end with spaces.
Return the number of letters in the last word of s, that is the word that is furthest to the right. The string always contains at least one word.
Examples
Example 1
Input: s = "good morning"
Output: 7
The last word is morning, which has 7 letters.
Example 2
Input: s = " fly me to the moon "
Output: 4
Trailing spaces do not count. The last word is moon.
Example 3
Input: s = "single"
Output: 6
Constraints
- 1 <= len(s) <= 10^4
- s contains only English letters and the space character.
- s contains at least one word.