We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 7aa0841 commit 28df400Copy full SHA for 28df400
scripts/algorithms/D/Destination City/Destination City.cpp
@@ -1,24 +1,16 @@
1
class Solution {
2
public:
3
string destCity(vector<vector<string>>& paths) {
4
- string ans="";
5
- int flag=0;
6
- for(int i=0;i<paths.size();i++){
7
- for(int j=0;j<paths.size();j++){
8
- if(paths[i][1]==paths[j][0])
9
- break;
10
- else{
11
-
12
- if(j==paths.size()-1){
13
- ans+=paths[i][1];
14
- flag=1;
15
16
- }
17
18
19
- if(flag==1)
20
+ string ans;
+ unordered_map<string, int> m;
+ for(int i=0; i<paths.size(); i++){
+ m[paths[i][0]]++;
+ m[paths[i][1]]--;
21
}
+ for(auto city : m){
+ if(city.second == -1) ans = city.first;
+ }
+
22
return ans;
23
24
-};
+};
0 commit comments