Loading content...
You are given a collection of airline flight segments represented as pairs [origin, destination], where each pair indicates a direct flight from the origin airport to the destination airport. Your task is to construct a complete travel itinerary that uses every flight segment exactly once.
All flight segments belong to a single traveler whose journey must begin at the airport with code "JFK". Therefore, your reconstructed itinerary must start from "JFK".
When multiple valid itineraries exist (i.e., multiple ways to use all the flights), you must return the itinerary that is lexicographically smallest when read as a sequence of airport codes. In other words, among all possible valid routes, prefer the one where, at each step, you choose the alphabetically earliest available destination.
Key Observations:
n + 1 airport codes, where n is the number of flight segmentsflights = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]["JFK","MUC","LHR","SFO","SJC"]Starting from JFK, we fly to MUC, then to LHR, then to SFO, and finally to SJC. This uses all 4 flight segments exactly once and forms a valid connected itinerary.
flights = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]["JFK","ATL","JFK","SFO","ATL","SFO"]From JFK, we could go to either ATL or SFO. Choosing ATL first (lexicographically smaller) leads to the path: JFK → ATL → JFK → SFO → ATL → SFO. An alternative route ["JFK","SFO","ATL","JFK","ATL","SFO"] also uses all flights but is lexicographically larger.
flights = [["JFK","LAX"],["LAX","SFO"],["SFO","ORD"]]["JFK","LAX","SFO","ORD"]A simple linear path from JFK through LAX and SFO to ORD, using all three flight segments in sequence.
Constraints