Length of the Last Word in a Sentence

Implement lengthOfLastWord

Given a string s consisting of words separated by spaces (possibly with trailing spaces), return the length of the last word — a maximal substring of non-space characters. Splitting the whole string into words works, but it does more than the question asks: every earlier word gets extracted and thrown away, just to reach the last one. Scanning backward from the end skips straight past the trailing spaces to the last word and counts it directly, without ever building the words that came before it.

Example 1:

Input: s = "Hello World "

Output: 5

Example 2:

Input: s = " fly me to the moon "

Output: 4

Example 3:

Input: s = "luffy is still joyboy"

Output: 6

+ 7 hidden test cases run on Submit.

Constraints:

  • 1 ≤ s.length ≤ 10⁴
  • s consists of English letters and spaces ' '
  • There is at least one word in s
  • s may have trailing spaces

s =

Hello World