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
Inputs = "Hello World"
Output5
The last word is World, which has length 5.
Example 2
Inputs = " fly me to the moon "
Output4
After 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

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate