You are given a list paths, where paths[i] = [city_a, city_b] means there is a direct path going from city_a to city_b. Return the destination city, that is, the city without any path going out to another city.
The paths are guaranteed to form a single line without any loop, so there is exactly one destination city.
Function Signature
def dest_city(paths: list[list[str]]) -> str:
Rules
-
Each entry of
paths
is a two-element list
[origin, destination]
describing one directed path.
-
The entries can appear in any order; they are not listed in travel order.
-
City names are compared as exact strings. They are case-sensitive and may contain spaces (
"New York"
is one city).
-
Return the name of the destination city as a string.
Constraints
-
1 <= len(paths) <= 100
-
len(paths[i]) == 2
-
1 <= len(city_a), len(city_b) <= 10
-
city_a != city_b
-
All strings consist of lowercase and uppercase English letters and the space character.
Examples
Input: paths = [["London", "New York"], ["New York", "Lima"], ["Lima", "Sao Paulo"]]
Output: "Sao Paulo"
The path graph is London -> New York -> Lima -> Sao Paulo. Sao Paulo is the only city with no outgoing path.
Input: paths = [["B", "C"], ["D", "B"], ["C", "A"]]
Output: "A"
The paths are given out of order, but they form the line D -> B -> C -> A. B, C and D each start a path, and A does not.
Input: paths = [["A", "Z"]]
Output: "Z"
With a single path, the city it ends at has nothing going out of it.