Close
Topic list
Reconstruct Itinerary
MEDIUM
15 mins
Matrices (2D Arrays)
Graph
Topics (Covered in this problem)
Problem solved
Skill meter
Matrices (2D Arrays)
-
-
Graph
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Reconstruct Itinerary

Contributed by
Deep Mavani
Medium
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share

## Problem Statement

#### 3. He must use all the tickets only once.

##### Note:
``````1. Journey means the order in which the cities will be visited.

2. The given tickets have at least one itinerary.

3. If multiple valid itineraries are possible, then return the itinerary that is a lexicographically smallest itinerary.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````1
3
DEL KUL
DEL NRT
NRT DEL
``````
##### Sample Output 1:
``````DEL NRT DEL KUL
``````
##### Explanation for Sample Input 1:
``````In test case 1, there are three flight tickets.

Source    -     Destination
DEL     -       KUL
DEL      -  NRT
NRT    -     DEL

In order to satisfy all the conditions, he should start from “DEL”, as per the problem statement, use ticket 2, go to “NRT”. Then come back to “DEL” using ticket 3. And then go to “KUL” using ticket 1. So the Itinerary which is possible in this case is DEL -> NRT -> DEL -> KUL.
``````
##### Sample Input 2:
``````1
2
DEL ATL
ATL DEL
``````
##### Sample Output 1:
``````DEL ATL DEL
``````
##### Explanation for Sample Input 2:
``````In test case 1, there are two flight tickets.

Source  -   Destination
DEL     -    ATL
ATL     -    DEL

In order to satisfy all the conditions, he should start from “DEL”, as per the problem statement, use ticket 1, go to “ATL”. Then come back to “DEL” using ticket 2. So the Itinerary which is possible in this case is DEL -> ATL -> DEL.
``````
Auto
Console