Problem
You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.
A 0-indexed string num of length n + 1 is created using the following conditions:
numconsists of the digits'1'to'9', where each digit is used at most once.- If
pattern[i] == 'I', thennum[i] < num[i + 1]. - If
pattern[i] == 'D', thennum[i] > num[i + 1].
Return the lexicographically smallest possible string num that meets the conditions._
Examples
Example 1:
| |
Example 2:
| |
Constraints:
1 <= pattern.length <= 8patternconsists of only the letters'I'and'D'.
Solution
Here are the initial observations:
- The sequence
I(Increasing) means the next number should be greater than the current. - The sequence
D(Decreasing) means the next number should be smaller than the current.
Video explanation
Here is the video explaining this method in detail. Please check it out:
Method 1 - Brute Force
The most naive strategy would be to generate all possible strings of length n + 1 consisting of digits 1 to 9 and then verify which of them satisfies the conditions defined by the pattern. From all valid strings, we would return the smallest one (lexicographically).
Complexity
- ⏰ Time complexity:
O(9^n)- For each digit, we have
9possible choices. - With a string length of
n + 1, there are as many as9^(n+1)possible combinations.
- For each digit, we have
- 🧺 Space complexity:
O(n), due to recursion stack.
Method 2 - Using Backtracking
The naive approach involves generating all permutations of the digits {1, 2, ..., 9} of size n + 1, and then checking which permutation satisfies the constraints given by pattern. From these valid permutations, we’d choose the lexicographically smallest one.
Here is the approach:
- Generate Permutations:
- Use recursion to generate all permutations of integers
1ton + 1(only use each digit once).
- Use recursion to generate all permutations of integers
- Validate Each Permutation:
- For a given permutation, traverse the
patterncharacter by character:- If the current character is
I, check ifperm[i] < perm[i+1]. - If the current character is
D, check ifperm[i] > perm[i+1].
- If the current character is
- If any of these checks fail, discard this permutation.
- For a given permutation, traverse the
- Track the Smallest Valid Permutation:
- Keep track of the lexicographically smallest valid permutation as you process each permutation.
- Return the smallest valid permutation at the end.
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n! * n)- The recursive approach generates all permutations of
1, 2, ..., n + 1. - The number of permutations is
(n + 1)!. - For each permutation, validating it against the
patterntakesO(n)time. - Total complexity is approximately
O((n+1)! * n).
- The recursive approach generates all permutations of
- 🧺 Space complexity:
O(n)
Method 3 - Using Stack
The key idea is:
- When encountering
D, delay placing the digits until the sequence changes toIor we reach the end of the string. This ensures the digits are added in reverse order (largest first) for these decreasing groups.
Here is the approach:
- Iterate through the pattern while maintaining a stack.
- Push the current digit from
1to9into the stack. - Whenever an
Iis encountered or we reach the end of the pattern, pop all elements from the stack and append to the result. This ensures the correct order for decreasing groups. - Repeat this process until all characters in the pattern are processed.
Code
| |
| |
Complexity
- ⏰ Time complexity:
O(n), as each digit is pushed and popped from the stack exactly once. - 🧺 Space complexity:
O(n)for using stack and storing result.