Question
Java Find Itinerary in Order Description Given a list of tickets in pairs as source city and destination city, find the itinerary in order using
Java Find Itinerary in Order
DescriptionGiven a list of tickets in pairs as source city and destination city, find the itinerary in order using the given list.
Note that the given list of tickets is non-cyclic and there is one ticket from each city except the destination city.
Input Format:
The first line contains an integerNrepresenting the number of tickets.
For the nextNlines,each line contains a ticket representing the source city and the destination city.
Output Format:
Print Invalid Input if the start city isnull,else print city in an itinerary in order, a pair in each line as shown below:
Bangalore->Ranchi
Ranchi->Jamshedpur
Sample Test Cases:
Input:
4
Bangalore Ranchi
Jamshedpur Kolkata
Ranchi Jamshedpur
Kolkata Varanasi
Output:
Bangalore->Ranchi
Ranchi->Jamshedpur
Jamshedpur->Kolkata
Kolkata->Varanasi
Input:
4
Chennai Bangalore
Bombay Delhi
Goa Chennai
Delhi Goa
Output:
Bombay->Delhi
Delhi->Goa
Goa->Chennai
Chennai->Bangalore
import java.util.*;
public class Source {
public static void findItinerary(Map tickets) { // Write code here },>
public static void main(String[] args) { Map tickets = new HashMap(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0 ; i tickets.put(sc.next(),sc.next()); } findItinerary(tickets); } },>,>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started