Destination City
Real-world version: Last Stop On The Courier RunThe 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.
def final_stop(legs: list[list[str]]) -> str:public String finalStop(List<List<String>> legs)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
More on the same topics
- Verifying an Alien DictionaryReal-world version: Catalogue Order Under a Custom Glyph SequenceEasy
- Find Common CharactersReal-world version: Tiles Every Rack Can SupplyEasy
- Find Words That Can Be Formed by CharactersReal-world version: Signs Spellable From A Tile TrayEasy
- Sort the PeopleReal-world version: Season Leaderboard From Two Parallel ListsEasy