Reconstruct Itinerary
You are given a list of airline tickets where tickets[i] = [from, to]. Reconstruct the itinerary that uses all tickets exactly once, starting from 'JFK'. If multiple valid itineraries exist, return the one with the smallest lexical order when read as a single string. A valid itinerary is guaranteed to exist.
Open official problem prompt ↗Order all the tickets into one continuous trip from JFK that uses each ticket exactly once and is lexicographically smallest.
Like tracing a route through a subway map where you must ride every line exactly once; when you hit a station with no unridden lines left, you've found the tail end of your journey.
- Input
- tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
- Output
- ["JFK","MUC","LHR","SFO","SJC"]
- Why
- Starting at JFK, this is the only ordering that consumes every ticket exactly once.
1 <= tickets.length <= 300tickets[i].length == 2from_i, to_i are 3 uppercase lettersfrom_i != to_iA valid itinerary using all tickets exists