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,
It may be assumed that the input list of tickets is not cyclic and there is one ticket from every city except final destination.
One Solution is to build a graph and do Topological Sorting of the graph. Time complexity of this solution is O(n).
We can also use hashing to avoid building a graph. The idea is to first find the starting point. A starting point would never be on ‘to’ side of a ticket. Once we find the starting point, we can simply traverse the given map to print itinerary in order. Following are steps.
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.
All of the above steps require O(n) time so overall time complexity is O(n).
Below is Java implementation of above idea.
Bombay->Delhi, Delhi->Goa, Goa->Chennai, Chennai->Banglore,
This article is compiled by Rahul Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the element in a linked list with frequency at least N/3
- Find unique elements in linked list
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- First non-repeating in a linked list
- Linked List Pair Sum
- Majority element in a linked list
- Count duplicates in a given linked list
- Absolute distinct count in a Linked List
- Product of divisors of a number from a given list of its prime factors
- Check if a pair with given product exists in Linked list
- Count minimum frequency elements in a linked list
- Sort the linked list in the order of elements appearing in the array
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Find four elements that sum to a given value | Set 2
- Find all triplets with zero sum
- Find if there is a subarray with 0 sum
- Find a pair with given sum in BST
- Find substrings that contain all vowels
- Find three element from different three arrays such that a + b + c = sum
- Find top three repeated in array