BackmediumRecursion Infosys

Galactic Navigation Sequences Solution

Problem Statement

Write a function to generate all possible valid combinations of n hyperjumps and hyperslows, represented as 'J' and 'S' respectively, and return them as a list of strings.

Example 1
Input
n = 3
Output
['J', 'S', 'JJ', 'JS', 'JJJ', 'JJJS', 'JJJJ', 'JJJJS', 'JJJJJ', 'JJJJJS']

Explanation: Step-by-step: We start with an empty string. We have two choices for each character: 'J' or 'S'. We can generate all possible combinations by recursively adding 'J' or 'S' to the current string, up to n times.

Example 2
Input
n = 4
Output
['JSSJJJJ', 'JJSSJJJJ', 'JSSSJJJJ', 'JJSSSJJJJ', 'JSSJJJSSJJ', 'JJSSJJJSSJJ', 'JSSSJJJSSJJ', 'JJSSSJJJSSJJ', 'JSSJJJJSSJJ', 'JJSSJJJJSSJJ']

Explanation: Step-by-step: We start with an empty string. We have two choices for each character: 'J' or 'S'. We can generate all possible combinations by recursively adding 'J' or 'S' to the current string, up to n times.

Constraints

  • 1 <= n <= 10, where n is the number of pairs of hyperjumps and hyperslows
  • The output list should not contain any duplicate combinations
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free