A travel app imports a pile of used tickets, each [from, to] with place codes as strings, and must rebuild the whole trip. The trip starts at start and uses every ticket exactly once, in some order where each ticket starts where the previous one ended.
Write rebuildTrip(tickets, start) that returns the list of places visited, in order. If several trips are possible, return the one that comes first when comparing the lists place by place in plain string order. At least one valid trip always exists.
Input: tickets = [["HOME","LON"],["LON","PAR"],["PAR","HOME"],["HOME","ROM"]], start = "HOME" Output: ["HOME","LON","PAR","HOME","ROM"]
Going to ROM first would leave the other three tickets unused, since nothing leaves ROM. So the loop through LON and PAR comes first.
Input: tickets = [["A","B"],["A","C"],["C","A"]], start = "A" Output: ["A","C","A","B"]
B comes before C alphabetically, but going to B first gets stuck with tickets left.
1 <= tickets.length <= 300Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #480 Rebuild the Trip From Tickets. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.