-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDestinationCity.java
More file actions
45 lines (35 loc) · 1.19 KB
/
Copy pathDestinationCity.java
File metadata and controls
45 lines (35 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package LeetcodePractice;
import java.util.*;
public class DestinationCity {
public static String destCity(List<List<String>> paths) {
Set<String> startingCities = new HashSet<>();
for (List<String> path : paths) {
startingCities.add(path.get(0));
}
for (List<String> path : paths) {
String endCity = path.get(1);
if (!startingCities.contains(endCity)) {
return endCity;
}
}
return "";
}
public static void main(String[] args) {
List<List<String>> paths1 = Arrays.asList(
Arrays.asList("London","New York"),
Arrays.asList("New York","Lima"),
Arrays.asList("Lima","Sao Paulo")
);
System.out.println(destCity(paths1));
List<List<String>> paths2 = Arrays.asList(
Arrays.asList("B","C"),
Arrays.asList("D","B"),
Arrays.asList("C","A")
);
System.out.println(destCity(paths2));
List<List<String>> paths3 = Arrays.asList(
Arrays.asList("A","Z")
);
System.out.println(destCity(paths3));
}
}