Find Permutation - Problem

Imagine you're a detective decoding a secret message! You have a cryptic string that describes the relationship between consecutive numbers in a hidden permutation.

Given a string s of length n-1 containing only characters 'I' (Increasing) and 'D' (Decreasing), you need to reconstruct the lexicographically smallest permutation of integers from [1, n] that matches this pattern.

The Rules:

  • s[i] == 'I' means perm[i] < perm[i+1] (next number is larger)
  • s[i] == 'D' means perm[i] > perm[i+1] (next number is smaller)

Example: If s = "DI", you need a 3-number permutation where the first number is greater than the second, and the second is less than the third. The answer is [2,1,3] because it's the lexicographically smallest valid permutation.

Input & Output

example_1.py โ€” Basic case
$ Input: s = "I"
โ€บ Output: [1, 2]
๐Ÿ’ก Note: We need perm[0] < perm[1]. The lexicographically smallest is [1, 2].
example_2.py โ€” Mixed pattern
$ Input: s = "DI"
โ€บ Output: [2, 1, 3]
๐Ÿ’ก Note: We need perm[0] > perm[1] and perm[1] < perm[2]. The pattern D-I creates a valley shape, so we get [2, 1, 3].
example_3.py โ€” All decreasing
$ Input: s = "DDI"
โ€บ Output: [3, 2, 1, 4]
๐Ÿ’ก Note: Three consecutive D's followed by I. We place largest numbers first (3,2,1) then the final ascending number (4).

Visualization

Tap to expand
Pattern Reconstruction VisualizationInput: s = "DI" โ†’ Need permutation of [1,2,3]StartD(Down)I(Up)EndStack Operations:Initial Stack[ ]After 'D'[0, 1]Delay assignmentAfter 'I'Pop: 1โ†’1Pop: 0โ†’2Assign: 2โ†’3Final Result[2, 1, 3]Lexicographically smallest!โœ… Verification:perm[0] > perm[1]? โ†’ 2 > 1 โœ“perm[1] < perm[2]? โ†’ 1 < 3 โœ“๐ŸŽฏ Key Insight: Stack delays assignment during decreasing sequences, ensuring optimal ordering
Understanding the Visualization
1
Initialize
Start with an empty result array and a stack
2
Process Each Position
For each position, push its index to the stack
3
Handle 'I' or End
When we see 'I' or reach the end, pop indices from stack and assign ascending numbers
4
Result
The stack ensures we assign the smallest possible numbers while satisfying the pattern
Key Takeaway
๐ŸŽฏ Key Insight: The stack-based approach elegantly handles the constraint that we need the lexicographically smallest permutation by delaying number assignments during decreasing sequences and then assigning them in the optimal order.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n! * n)

Generate n! permutations, each taking O(n) time to validate

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Space for storing current permutation and result

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists only of characters 'D' and 'I'
  • The result should be lexicographically smallest among all valid permutations
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
41.2K Views
Medium Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen