Skip to content
Decoder

Length of Last Word

Easy

Problem

A string s holds words separated by spaces. There may be extra spaces at the start, at the end, or between words. A word is a run of non-space characters with no space inside it. Return the length of the last word in s.

Example 1

Input
s = "the quick fox"
Output
3
Why
"fox"

Example 2

Input
s = " hold the line "
Output
4
Why
"line"

Example 3

Input
s = "a"
Output
1

Constraints

  • 1 <= s.length <= 10^4
  • s contains only English letters and spaces.
  • s contains at least one word.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.