All problems
0167EasyString

Trailing Tag Length

Tracked in this browser only
Write code

Trains the technique from

LeetCode 58Length of Last Word

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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