Show problems as
Classic problem #1436EasyArrayHash TableString

Destination City

Real-world version: Last Stop On The Courier Run
Tracked in this browser only
Read Destination City on LeetCode(opens in a new tab)

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

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

Python
def final_stop(legs: list[list[str]]) -> str:
Java
public String finalStop(List<List<String>> legs)