BackmediumRecursionPayPal

Balanced Hyperjump Sequences Solution

Problem Statement

Generate all possible valid combinations of 'J' (hyperjump) and 'S' (hyperslow) sequences of length n, where every 'J' has a corresponding 'S', and no sequence is a substring of another. Return these combinations as a list of strings.

Example 1
Input
n = 5
Output
['JJJJSSSSSS', 'JJJJSSSSSJ', 'JJJJSSSSSJSS', 'JJJJSSSSSJSSS', 'JJJJSSSSSJSSSS', 'JJJJSSSSSJSSSSS', 'JJJJSSSSSJSSSSSS', 'JJJJSSSSSJSSSSSSS', 'JJJJSSSSSJSSSSSSSS', 'JJJJSSSSSJSSSSSSSSS']

Explanation: Step-by-step: 1. Generate all possible combinations of 'J' and 'S' sequences of length n. 2. Check each sequence to ensure every 'J' has a corresponding 'S' and no sequence is a substring of another. 3. Return the valid combinations as a list of strings.

Example 2
Input
n = 3
Output
['JJJJ', 'JJJJS', 'JJJJSJ', 'JJJJSJS', 'JJJJSJSJJJ']

Explanation: Step-by-step: 1. Generate all possible combinations of 'J' and 'S' sequences of length n. 2. Check each sequence to ensure every 'J' has a corresponding 'S' and no sequence is a substring of another. 3. Return the valid combinations as a list of strings.

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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Balanced Hyperjump Sequences — Problem Statement & Solution Guide

RecursionMediumMixed
TimeO(2^n * n)
|
SpaceO(2^n)

Problem Description

Generate all possible valid combinations of 'J' (hyperjump) and 'S' (hyperslow) sequences of length n, where every 'J' has a corresponding 'S', and no sequence is a substring of another. Return these combinations as a list of strings.

Examples

Example 1

Input

n = 5

Output

['JJJJSSSSSS', 'JJJJSSSSSJ', 'JJJJSSSSSJSS', 'JJJJSSSSSJSSS', 'JJJJSSSSSJSSSS', 'JJJJSSSSSJSSSSS', 'JJJJSSSSSJSSSSSS', 'JJJJSSSSSJSSSSSSS', 'JJJJSSSSSJSSSSSSSS', 'JJJJSSSSSJSSSSSSSSS']

Explanation: Step-by-step: 1. Generate all possible combinations of 'J' and 'S' sequences of length n. 2. Check each sequence to ensure every 'J' has a corresponding 'S' and no sequence is a substring of another. 3. Return the valid combinations as a list of strings.

Example 2

Input

n = 3

Output

['JJJJ', 'JJJJS', 'JJJJSJ', 'JJJJSJS', 'JJJJSJSJJJ']

Explanation: Step-by-step: 1. Generate all possible combinations of 'J' and 'S' sequences of length n. 2. Check each sequence to ensure every 'J' has a corresponding 'S' and no sequence is a substring of another. 3. Return the valid combinations as a list of strings.

Constraints

  • 1 <= n <= 10, where n is the number of pairs of hyperjumps and hyperslows
  • The output list should not contain any duplicate combinations

Optimal Approach & Strategy

A more efficient approach would be to use recursion to build up valid combinations. This approach would have a time complexity of O((2n)! / (n! * n!)) and would be much more efficient than the naive approach.

Brute Force Approach

One naive approach would be to generate all possible combinations of 'J' and 'S' and then filter out the combinations that are not valid. However, this approach would have a time complexity of O(2^(2n)) and would be very inefficient for large inputs.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

PayPal

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.