A permutation perm of n integers of all the integers in the range [1, n] can be represented as a string s of length n - 1 where:
s[i] == 'I' if perm[i] < perm[i + 1], ands[i] == 'D' if perm[i] > perm[i + 1].Given a string s, reconstruct the lexicographically smallest permutation perm and return it.
Example 1:
Input: s = "I" Output: [1,2] Explanation: [1,2] is the only legal permutation that can represented by s, where the number 1 and 2 construct an increasing relationship.
Example 2:
Input: s = "DI" Output: [2,1,3] Explanation: Both [2,1,3] and [3,1,2] can be represented as "DI", but since we want to find the smallest lexicographical permutation, you should return [2,1,3]
Constraints:
1 <= s.length <= 105s[i] is either 'I' or 'D'.Problem Overview: You are given a string consisting of characters 'I' (increasing) and 'D' (decreasing). The goal is to construct the lexicographically smallest permutation of numbers 1..n that satisfies this pattern. If s[i] = 'I', then perm[i] < perm[i+1]; if 'D', then perm[i] > perm[i+1].
Approach 1: Brute Force Permutation Search (O(n! * n) time, O(n) space)
Generate all permutations of numbers 1..n and check which ones satisfy the pattern. For each permutation, iterate through the pattern and verify whether adjacent elements follow the required increasing or decreasing rule. The first valid permutation in lexicographical order is the answer. This approach works conceptually but becomes infeasible quickly because the number of permutations grows factorially.
Approach 2: Greedy with Segment Reversal (O(n) time, O(1) extra space)
Start by building a sequence [1,2,3,...,n]. Traverse the pattern and look for consecutive 'D' segments. Whenever a block of 'D' appears from index i to j, reverse the corresponding range in the permutation. Reversing transforms the increasing sequence into the required decreasing structure while keeping the permutation lexicographically minimal. This greedy idea works because the smallest numbers are placed as early as possible while only adjusting ranges that violate the pattern.
Approach 3: Stack-Based Greedy (O(n) time, O(n) space)
This is the most common interview solution. Iterate through indices from 1 to n and push each number onto a stack. Whenever you encounter an 'I' or reach the end of the pattern, pop all elements from the stack and append them to the result. The stack naturally reverses segments corresponding to 'D' runs, producing decreasing subsequences while preserving the smallest available numbers. The algorithm performs one pass through the pattern and uses stack push/pop operations, making it both simple and efficient.
The stack and reversal strategies rely on the same greedy insight: delay output while reading 'D', then release numbers in reverse order to enforce decreasing constraints. This pattern manipulation frequently appears in stack and greedy problems involving sequences.
Recommended for interviews: The stack-based greedy solution is what most interviewers expect. It runs in O(n) time with O(n) space and clearly demonstrates understanding of pattern processing. Mentioning the brute force approach shows baseline reasoning, but implementing the greedy stack solution proves you can optimize using insights about array ordering and pattern constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutations | O(n! * n) | O(n) | Conceptual understanding or very small input sizes |
| Greedy with Segment Reversal | O(n) | O(1) | Efficient in-place solution when modifying an array directly |
| Stack-Based Greedy | O(n) | O(n) | Most intuitive approach for interviews and editorial explanations |
LeetCode 484. Find Permutation • Happy Coding • 1,545 views views
Watch 5 more video solutions →Practice Find Permutation with our built-in code editor and test cases.
Practice on FleetCode