mediumBacktrackingPattern: Mixed

Travel Itinerary Combinations Solution

Problem Statement

Given a list of cities and their corresponding airports, find all possible travel itineraries such that each traveler visits exactly two cities.

Examples

Example 1:
Input:{"cities":["City A","City B","City C"],"airports":["Airport A","Airport B","Airport C"]}
Output:[["City A","City B"],["City A","City C"],["City B","City A"],["City B","City C"],["City C","City A"],["City C","City B"]]
Explanation: Each traveler visits exactly two cities

Constraints

  • Each city and airport code is a string in the format 'city-code' with no duplicates.
  • All travel itineraries consist of exactly two connected cities.
  • Cities that have been visited are not re-visited.
  • The number of travelers is fixed at 3 or N.
Time: O(n*n!) Space: O(n)
Analyze constraints and compute optimal solutions step-by-step.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.