-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathticketProblem.java
More file actions
57 lines (46 loc) · 1.33 KB
/
ticketProblem.java
File metadata and controls
57 lines (46 loc) · 1.33 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
47
48
49
50
51
52
53
54
55
56
57
/*
Question -:
Find Itinerary from a given list of tickets
Given a list of tickets, find itinerary in order using the given list.
Input:
"Chennai" -> "Banglore"
"Bombay" -> "Delhi"
"Goa" -> "Chennai"
"Delhi" -> "Goa"
Output:
Bombay->Delhi, Delhi->Goa, Goa->Chennai, Chennai->Banglore,
*/
import java.util.HashMap;
import java.util.Map;
public class ticketProblem {
public static void ticket() {
HashMap<String, String> data = new HashMap<String, String>();
data.put("CHE", "BAN");
data.put("BOM", "DEL");
data.put("GOA", "CHE");
data.put("DEL", "GOA");
HashMap<String, String> reverseData = new HashMap<String, String>();
data.forEach((h, k) -> {
reverseData.put(k, h);
});
String start = null;
for (Map.Entry<String, String> e : data.entrySet()) {
if (!reverseData.containsKey(e.getKey())) {
start = e.getKey();
}
}
String to = data.get(start);
for(Map.Entry<String,String> e: data.entrySet()){
while (to!=null){
System.out.print(start+ " -> "+to+" ,");
start = to;
to = data.get(start);
}
}
}
public static void main(String[] args) {
ticket();
}
}
/**
* @author Pradumn Patel */