Length of the Last Word
Problem statement
Given a string text containing English letters and spaces, return the length of its last word.
A word is a maximal contiguous sequence of non-space characters. The input contains at least one word and may have leading, trailing, or repeated spaces.
Function
lengthOfLastWord(text: String) → intExamples
Example 1
text = "Hello World"return = 5The last word is World, whose length is 5.
Example 2
text = " fly me to the moon "return = 4Trailing spaces are ignored. The last word is moon.
Example 3
text = "luffy is still joyboy"return = 6The final word is joyboy.
Constraints
1 <= text.length <= 100000textcontains only English letters and the space character.textcontains at least one word.