Ctrl K

Length of the Final Word

Easy
You are given a string text made of English letters and spaces. A word is a run of non-space characters that cannot be extended to the left or to the right. Return the length of the last word in the string. Spaces may appear at the start, at the end, and several in a row.
Example 1
text="Good morning everyone" → 8

The last word is everyone, which has 8 letters.

Example 2
text="  see you   later  " → 5

Trailing spaces do not count: the last word is later.

Example 3
text="Hi" → 2
Constraints
  • 1 ≤ len(text) ≤ 10⁴
  • text consists of English letters and spaces
  • text contains at least one word
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasySum of Binary Strings Easy
loading Python…
Input
text="Good morning everyone"
Expected
8

Run checks the first 3 tests in your browser. Submit checks all 6 tests on the server, hidden ones included.