You are given a string 'S' that consists of I’s and D’s pattern only. 'I' is for increasing, and 'D' is for decreasing. Your task is to return the string that consists of a minimum number following that pattern.
Note:
1) Digits of the number should be in the range [1,9] and can’t repeat.
2) The length of the string should be less than or equal to 8.
The first line of the input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains a string 'S', as described in the problem statement.
Output Format:
For each test case, return a string consisting of a minimum number following the given pattern.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10^3
1 <= |S| <= 8
where |S| is the length of the given string.
Time Limit: 1sec
1
IIID
12354
The pattern consists of increasing up to 3 places and decreasing up to 2 places. Numbers following that pattern are 12354, 23465, 46785, etc. The minimum among all of them is 12354.
1
DID
2143
The pattern consists of decreasing up to 1 place and then increasing up to 1 place and at last, decreasing up to 1 place. Numbers following that pattern are 4231, 2143, 9685, etc. The minimum among all of them is 2143.
Try to use a data structure that uses LIFO(Last In First Out) .
O(N), where ‘N’ is the length of the string ‘S’.
As we are traversing the input string ‘S’ only 1 time.
O(N), where ‘N' is the length of the string ‘S’.
As we are using a stack, which can go maximum up to ‘N’.