Destination City - Problem
You are given an array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi.
Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Input & Output
Example 1 — Basic Chain
$
Input:
paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
›
Output:
"Sao Paulo"
💡 Note:
Following the path: London → New York → Lima → Sao Paulo. Sao Paulo has no outgoing path, so it's the destination.
Example 2 — Simple Path
$
Input:
paths = [["B","C"],["D","B"],["C","A"]]
›
Output:
"A"
💡 Note:
The path is D → B → C → A. City A appears as a destination but never as a source.
Example 3 — Single Path
$
Input:
paths = [["A","Z"]]
›
Output:
"Z"
💡 Note:
Only one path A → Z, so Z is the destination city with no outgoing paths.
Constraints
- 1 ≤ paths.length ≤ 100
- paths[i].length == 2
- 1 ≤ cityAi.length, cityBi.length ≤ 10
- cityAi ≠ cityBi
- All strings consist of lowercase and uppercase English letters and the space character.
Visualization
Tap to expand
Understanding the Visualization
1
Input Paths
Array of city pairs representing directed paths
2
Find Pattern
Destination city has no outgoing path
3
Output
Return the city that only appears as destination
Key Takeaway
🎯 Key Insight: The destination city is the only city that appears as an arrival but never as a departure in any path
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code