-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob12.java
83 lines (73 loc) · 2.2 KB
/
prob12.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.HashMap;
import java.util.Map;
/**
* Find Itinerary from a given list of tickets
*
*
* Input:
* "Chennai" -> "Banglore"
* "Bombay" -> "Delhi"
* "Goa" -> "Chennai"
* "Delhi" -> "Goa"
*
* Output:
* Bombay->Delhi, Delhi->Goa, Goa->Chennai, Chennai->Banglore,
*
* 1) Create a HashMap of given pair of tickets. Let the created
* HashMap be 'dataset'. Every entry of 'dataset' is of the form
* "from->to" like "Chennai" -> "Banglore"
*
* 2) Find the starting point of itinerary.
* a) Create a reverse HashMap. Let the reverse be 'reverseMap'
* Entries of 'reverseMap' are of the form "to->form".
* Following is 'reverseMap' for above example.
* "Banglore"-> "Chennai"
* "Delhi" -> "Bombay"
* "Chennai" -> "Goa"
* "Goa" -> "Delhi"
*
* b) Traverse 'dataset'. For every key of dataset, check if it
* is there in 'reverseMap'. If a key is not present, then we
* found the starting point. In the above example, "Bombay" is
* starting point.
*
* 3) Start from above found starting point and traverse the 'dataset'
* to print itinerary.
*
*/
public class prob12 {
private static void printResult(Map<String, String> dataset) {
Map<String, String> reverseMap = new HashMap<String, String>();
for (Map.Entry<String, String> entry : dataset.entrySet()) {
reverseMap.put(entry.getValue(), entry.getKey());
}
String start = null;
for (Map.Entry<String, String> entry : dataset.entrySet()) {
if (!reverseMap.containsKey(entry.getKey())) {
start = entry.getKey();
break;
}
}
if (start == null) {
System.out.println("Invalid Input");
return;
}
// Once we have starting point, we simple need to go next, next
// of next using given hash map
String to = dataset.get(start);
while (to != null) {
System.out.print(start + "->" + to + ", ");
start = to;
to = dataset.get(to);
}
}
// Driver function
public static void main(String[] args) {
Map<String, String> dataSet = new HashMap<String, String>();
dataSet.put("Chennai", "Banglore");
dataSet.put("Bombay", "Delhi");
dataSet.put("Goa", "Chennai");
dataSet.put("Delhi", "Goa");
printResult(dataSet);
}
}