hard

Reconstruct Itinerary

You are given a list of airline tickets as [from, to] pairs. Starting at JFK, build an itinerary that uses every ticket exactly once. If several itineraries are possible, return the one that is lexicographically smallest when read as a sequence of airports.

Constraints
  • 1 ≤ tickets.length ≤ 300
  • Airport codes are 3 uppercase letters
  • At least one valid itinerary exists
Examples
in: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
out: ["JFK","MUC","LHR","SFO","SJC"]

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.