Show problems as
Classic problem #58EasyString

Length of Last Word

Real-world version: Trailing Tag Length
Tracked in this browser only
Read Length of Last Word on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A dock scanner prints one line of routing text per pallet. The text is made only of English letters and blank spaces, and a run of letters with no blank inside it forms a single tag. Blanks are used purely as separators: the printer pads unpredictably, so a line can start with blanks, end with blanks, and carry several blanks between two neighbouring tags.

The rightmost tag on the line is the one the loader keys in, and the operator wants to know how many characters that tag occupies.

Report how many letters make up the final tag of line. Every line holds at least one tag, and padding blanks are never part of a tag.

Examples

Example 1

Input
line = "dock crew ready"
Output
5

The final tag is `ready`, which occupies five characters.

Example 2

Input
line = " loading bay "
Output
3

Padding sits at both ends of the print, and the rightmost tag is still `bay` with three letters.

Example 3

Input
line = "north pier"
Output
4

Three blanks separate the two tags, and the one on the right, `pier`, has four letters.

Constraints

  • 1 <= line.length <= 10^4
  • `line` contains English letters and the blank character ' ' only.
  • `line` holds at least one tag.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def trailing_tag_length(line: str) -> int:
Java
public int trailingTagLength(String line)