Skip to content

Length of Last Word

EasyTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

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.

s = "the quick fox"           →  3   ("fox")
s = "  hold   the line   "    →  4   ("line")
s = "a"                       →  1

Constraints

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