Destination City

You are given the array `paths`, where `paths[i] = [cityA_i, cityB_i]` means there is a direct road going from `cityA_i` to `cityB_i`. Return the destination city, that is, the city without any road leading out to another city. The graph of roads is guaranteed to form a line without any loop, so there is exactly one destination city.

30:00

Destination City
easy
Free written solution
Topics
Companies

You are given the array paths, where paths[i] = [cityA_i, cityB_i] means there is a direct road going from cityA_i to cityB_i. Return the destination city, that is, the city without any road leading out to another city.

The graph of roads is guaranteed to form a line without any loop, so there is exactly one destination city.

Example 1:
Input: {"paths":[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]}
Output: Sao Paulo
Constraints:
  • 1≤paths.length≤1001 \leq \text{paths.length} \leq 100

  • paths[i].length==2\text{paths}[i]\text{.length} == 2

  • 1≤cityAi.length,cityBi.length≤101 \leq \text{cityA}_i\text{.length}, \text{cityB}_i\text{.length} \leq 10

  • cityAi≠cityBi\text{cityA}_i \neq \text{cityB}_i

  • All strings consist of lowercase and uppercase English letters and the space character.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"paths":[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]}

There are 3 roads. Every city except the destination has a road leaving it, so first collect the start cities.

the trip (travel order)

→ = a road leaves this city

London

New York

Lima

Sao Paulo

paths (input order)

0

London

→

New York

1

New York

→

Lima

2

Lima

→

Sao Paulo

has_exit (size 0)

empty

Variables
VariableValue
roads3
len(has_exit)0
DepthFunction Call
Stack empty
0/7