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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!