Reconstruct Itinerary
Problem statement
You are given airline tickets represented as [from, to] pairs. Reconstruct the itinerary that uses every ticket exactly once.
The itinerary must start at "JFK". If multiple valid itineraries exist, return the lexicographically smallest itinerary when read as a single sequence of airport codes.
Function
findItinerary(tickets: List<List<String>>) → List<String>Examples
Example 1
tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]return = ["JFK","MUC","LHR","SFO","SJC"]There is only one route that uses all tickets from JFK.
Example 2
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]return = ["JFK","ATL","JFK","SFO","ATL","SFO"]Both choices from JFK can lead to valid routes, but the route beginning with ATL is lexicographically smaller.
Constraints
1 <= tickets.length <= 300.tickets[i].length == 2.- Each airport code has 1 to 3 uppercase English letters.
- The departure and arrival on a ticket differ.
- A complete itinerary that starts at
"JFK"and uses every ticket always exists.