Q2 PepCoding | Find Itinerary From Tickets

PHOTO EMBED

Mon Jan 23 2023 05:16:20 GMT+0000 (Coordinated Universal Time)

Saved by @Ayush_dabas07

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int noofpairs_src_des = scn.nextInt();
		HashMap<String, String> map = new HashMap<>();
		for (int i = 0; i < noofpairs_src_des; i++) {
			String s1 = scn.next();
			String s2 = scn.next();
			map.put(s1, s2);	
		}

		HashMap<String ,Boolean> set = new HashMap<>();
		
		//sp = starting point,ep=ending point 
		
		for(String sp : map.keySet()){
		    String ep = map.get(sp);
		    
		    set.put(ep,false);
		    if(set.containsKey(sp)==false){
		      set.put(sp,true);  
		    }
		}       
        
        String sp = "";
        for(String p : set.keySet()){
            if(set.get(p)==true){
                sp = p;
                break;
            }
            
        }
        
        while(true){
            if(map.containsKey(sp)==true){
                System.out.println(sp+" -> ");
                sp = map.get(sp);
            }
            else{
                System.out.println(sp+".");
                break
            }
        }
	}
}
content_copyCOPY

https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/hashmap-and-heaps/find-itinerary-from-tickets-official/ojquestion