-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeItinerary.java
More file actions
46 lines (38 loc) · 1.19 KB
/
Copy pathMakeItinerary.java
File metadata and controls
46 lines (38 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
46
package Arrays;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/*
https://www.techiedelight.com/find-itinerary-from-given-list-tickets/
*/
public class MakeItinerary {
public static void main(String[] args) {
// input: list of tickets
String[][] input = new String[][]{
{"LAX", "DXB"},
{"DFW", "JFK"},
{"LHR", "DFW"},
{"JFK", "LAX"}
};
solution(input);
}
public static void solution(String[][] itinerary) {
Map<String, String> map = Arrays.stream(itinerary)
.collect(Collectors.toMap(p -> p[0], p -> p[1]));
Set<String> airportsSet = new HashSet<>(map.values());
for (var array : itinerary) {
if (!airportsSet.contains(array[0])) {
display(map, array[0]);
break;
}
}
}
public static void display(Map<String, String> map, String source) {
while (map.containsKey(source)) {
System.out.println(source + " -> " + map.get(source));
source = map.get(source);
}
}
}