Length of Last Word
Real-world version: Trailing Tag LengthThe 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.
def trailing_tag_length(line: str) -> int:public int trailingTagLength(String line)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's