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'meansperm[i] < perm[i+1](next number is larger)s[i] == 'D'meansperm[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
Visualization
Time & Space Complexity
Generate n! permutations, each taking O(n) time to validate
Space for storing current permutation and result
Constraints
- 1 โค s.length โค 105
- s consists only of characters 'D' and 'I'
- The result should be lexicographically smallest among all valid permutations