Reconstruct Itinerary
You hold a set of airline tickets, each a pair [from, to] of three-letter airport codes. Use every ticket exactly once to build one trip that starts at "JFK", and return the airports in the order they are visited.
If several trips are possible, return the one that is smallest in lexicographic order, comparing the airport lists element by element (so ["JFK", "ATL", ...] beats ["JFK", "SFO", ...] because "ATL" < "SFO").
Tickets may repeat (the same flight twice means you must fly it twice). It is guaranteed that at least one valid trip exists.
Constraints
1 <= tickets.length <= 300- Each airport code is exactly three uppercase letters.
- A valid itinerary using all the tickets exists.
Examples
Example 1
Input: tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]] Output: ["JFK","MUC","LHR","SFO","SJC"]
Only one order uses every ticket: JFK to MUC to LHR to SFO to SJC.
Example 2
Input: tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]] Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
Two valid trips exist; JFK ATL JFK SFO ATL SFO is lexicographically smaller than JFK SFO ATL JFK ATL SFO.
Example 3
Input: tickets = [["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]] Output: ["JFK","NRT","JFK","KUL"]
KUL is smaller than NRT, but going there first strands the NRT to JFK ticket. The valid trip is JFK NRT JFK KUL.
JavaScript you'll use: Map and Set Sorting and comparators Graphs and grids in JavaScript
/**
* @param {string[][]} tickets
* @return {string[]}
*/
function findItinerary(tickets) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.