iq.lab
Python starts when a code cell comes near or you run one
easyLoops and basics target 15 min

Length of the last word

A string s is made of English letters and spaces. A word is a stretch of letters side by side, with a space or an end of s on each side: in "big data" the words are big and data, and dat is not a word. Spaces can sit at the start, at the end, or several in a row.

Return the number of letters in the last word of s. There is always at least one word.

Example 1
Inputs = "load the table "Output5

The last word is table. The two spaces after it are not a word.

Example 2
Inputs = " sort merge join"Output4

The two spaces at the start and the three between sort and merge change nothing: the last word is join.

Example 3
Inputs = "a"Output1

One word of one letter, starting at position 0.

Constraints
  • 1 ≤ len(s) ≤ 104

  • Every character is an English letter or a space.

  • At least one character is a letter.

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.