[LeetCode] Destination City

1436. Destination City

You are given the 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
string destCity(vector<vector<string>>& paths) {
unordered_set<string> ind, oud;
for(auto p : paths) {
auto src = p[0], dst = p[1];
ind.insert(src);
oud.insert(dst);
}
for(auto& o : oud) {
if(!ind.count(o)) return o;
}
return "";
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/15/PS/LeetCode/destination-city/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.