Trains the technique from
LeetCode 332Reconstruct ItineraryThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A regional courier holds a bundle of one-way haul permits. Each permit is written [origin, destination], a pair of three-letter uppercase depot codes, and it entitles the van to drive that leg exactly once. No permit names the same depot twice, and two permits may cover the very same leg, in which case that leg is driven twice.
The van sets out from the depot coded "HUB" and has to spend the whole bundle: every permit is driven exactly once. The bundle is always one that allows this.
Report the run as the list of depot codes the van stands at, in order: "HUB" first, then the destination of each leg as it is driven. The list therefore holds one more code than the bundle holds permits.
Several runs may spend the whole bundle. Return the smallest of them under front-to-back comparison: read two candidate runs position by position from the start, and the one holding the smaller depot code at the first position where they differ is the one to return.
Example 1
The run drives HUB to BEC, BEC back to HUB, then HUB to ARL, which spends all three permits and stands the van at four depots in turn.
Example 2
Each depot issues a single permit onward, so the bundle chains into one run and there is nothing to compare.
Example 3
Two runs spend the bundle: this one and the run that visits BEC before ARL. They first differ at position 1, where ARL is the smaller code.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def plan_hauls(permits: list[list[str]]) -> list[str]:public List<String> planHauls(List<List<String>> permits)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.