Tap here to switch tabs
Problem
Ask
Submissions

Problem: Reconstruct Itinerary

hard
40 min
Explore how to solve the reconstruct itinerary problem by applying graph algorithms and lexical ordering. Understand how to use all given airline tickets exactly once starting from JFK airport, while ensuring the itinerary follows the smallest lexicographical order. This lesson equips you to analyze problem statements and implement effective graph-based solutions useful for technical coding interviews.

Statement

Given a list of airline tickets where tickets[i] = [fromi, toi] represent a departure airport and an arrival airport of a single flight, reconstruct the itinerary in the correct order and return it.

The person who owns these tickets always starts their journey from "JFK". Therefore, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should prioritize the one with the smallest lexical ordeLexicographical order is a way of sorting similar to how words are arranged in a dictionary. It compares items character by character, based on their order in the alphabet or numerical value.r when considering a single string.

  • For example, the itinerary ["JFK", "EDU"] has a smaller lexical order than ["JFK", "EDX"].

Note: You may assume all tickets form at least one valid itinerary. You must use all the tickets exactly once.

Constraints:

  • 11 \leq tickets.length 300\leq 300

  • tickets[i].length =2= 2

  • fromi.length =3= 3

  • toi.length =3= 3

  • fromi \neq toi

  • fromi and toi consist of uppercase English letters.

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Reconstruct Itinerary

hard
40 min
Explore how to solve the reconstruct itinerary problem by applying graph algorithms and lexical ordering. Understand how to use all given airline tickets exactly once starting from JFK airport, while ensuring the itinerary follows the smallest lexicographical order. This lesson equips you to analyze problem statements and implement effective graph-based solutions useful for technical coding interviews.

Statement

Given a list of airline tickets where tickets[i] = [fromi, toi] represent a departure airport and an arrival airport of a single flight, reconstruct the itinerary in the correct order and return it.

The person who owns these tickets always starts their journey from "JFK". Therefore, the itinerary must begin with "JFK". If there are multiple valid itineraries, you should prioritize the one with the smallest lexical ordeLexicographical order is a way of sorting similar to how words are arranged in a dictionary. It compares items character by character, based on their order in the alphabet or numerical value.r when considering a single string.

  • For example, the itinerary ["JFK", "EDU"] has a smaller lexical order than ["JFK", "EDX"].

Note: You may assume all tickets form at least one valid itinerary. You must use all the tickets exactly once.

Constraints:

  • 11 \leq tickets.length 300\leq 300

  • tickets[i].length =2= 2

  • fromi.length =3= 3

  • toi.length =3= 3

  • fromi \neq toi

  • fromi and toi consist of uppercase English letters.