PIXELBANKv9.1.0
Menu

Given airline tickets as [from, to] pairs, reconstruct the itinerary starting from JFK. Use all tickets exactly once. If multiple valid itineraries, return the one with the smallest lexical order.

Output airports space-separated.

Example:

Input:
MUC:LHR,JFK:MUC,SFO:SJC,LHR:SFO
Output:
JFK MUC LHR SFO SJC
Reasoning:
  • The input is a list of airline tickets as [from, to] pairs: MUC:LHR, JFK:MUC, SFO:SJC, LHR:SFO
  • We start at JFK and look for a destination, finding JFK:MUC as the first flight
  • Then, from MUC, we find MUC:LHR, and from LHR, we find LHR:SFO, and finally from SFO, we find SFO:SJC, using all tickets exactly once
  • The resulting itinerary, JFK MUC LHR SFO SJC, is the one with the smallest lexical order among all possible valid itineraries

Constraints:

  • 1 <= tickets.length <= 300
  • tickets[i] = [from, to]
  • from and to are 3-letter airport codes
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Reconstruct Itinerary - Hard | PixelBank