-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequenceSourceAndDestination.java
More file actions
29 lines (28 loc) · 1.02 KB
/
SequenceSourceAndDestination.java
File metadata and controls
29 lines (28 loc) · 1.02 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
import java.util.*;
public class SequenceSourceAndDestination {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
int n = sc.nextInt();
HashMap<String, String> map = new HashMap<>();
HashSet<String> destinations = new HashSet<>();
for (int i = 0; i < n; i++) {
String source = sc.next();
String destination = sc.next();
map.put(source, destination);
destinations.add(destination);
}
String start = null;
for (String source : map.keySet()) {
if (!destinations.contains(source)) {
start = source;
break;
}
}
while (map.containsKey(start)) {
String destination = map.get(start);
System.out.println(start + " to " + destination);
start = destination;
}
}
}
}