Length of Last Word

Easy

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.

Example 1

lengthOfLastWord()
Input
s = "Hello World"
Output
5

Why

The last word is "World" with length 5.

Example 2

lengthOfLastWord()
Input
s = "   fly me   to   the moon  "
Output
4

Why

The last word is "moon" with length 4.
Constraints
1 <= s.length <= 10^4
s consists of only English letters and spaces.
There is at least one word in s.

Topics

Strings

Implement lengthOfLastWord in the editor and return the answer — printing it is never graded.