All problems
0721EasyArrayHash TableString

Last Stop On The Courier Run

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1436Destination City

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 courier's shift is written down as a list of legs. Entry legs[i] = [origin, arrival] records one leg driven from the depot named origin straight to the depot named arrival. The legs are jotted down in whatever order they were remembered, not in the order they were driven.

Taken together the legs form a single run that never returns to a depot it has already visited, so exactly one depot ends a leg without ever starting one.

Return the name of that depot, the stop where the run finishes. Depot names are compared character by character, so two names that differ only in letter case or in a space are different depots.

Examples

Example 1

Input
legs = [["Cedar", "Dune"], ["Alder", "Birch"], ["Birch", "Cedar"]]
Output
"Dune"

Alder, Birch and Cedar each start a leg. Dune does not, so the run ends there.

Example 2

Input
legs = [["Wren", "hollow"], ["hollow", "Hollow"]]
Output
"Hollow"

Wren and hollow each start a leg. Hollow with a capital H is a different depot and starts nothing, so the run ends there.

Example 3

Input
legs = [["Port Reed", "Kiln End"], ["Kiln End", "Ash Row"]]
Output
"Ash Row"

Port Reed and Kiln End each start a leg, while Ash Row only ever ends one, so the run finishes at Ash Row.

Constraints

  • 1 <= legs.length <= 100
  • legs[i].length == 2
  • 1 <= legs[i][j].length <= 10
  • The two depot names on one leg are different.
  • Depot names hold uppercase and lowercase English letters and the space character only.
  • The legs form one run that visits no depot twice, so exactly one depot never starts a leg.

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 final_stop(legs: list[list[str]]) -> str:
Java
public String finalStop(List<List<String>> legs)
September 7
Apply