Number following a pattern

Hard
0/120
Average time to solve is 15m
profile
Contributed by
15 upvotes
Asked in companies
Goldman SachsJosh Technology Group

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Constraints:
1 <= T <= 10^3
1 <= |S| <= 8

where |S| is the length of the given string.
Time Limit: 1sec
Sample Input 1:
1
IIID
Sample Output 1:
12354
Explanation for sample input 1:
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.
Sample Input 2:
1
DID       
Sample Output 2:
2143
Explanation for sample input 2:
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.
Hint

Try to use a data structure that uses LIFO(Last In First Out) .

Approaches (2)
Stack Based Approach
  • Create an empty stack and a string 'ANS' for storing the result.
  • In stack, we are going to push i+1every time for all 0 <= ‘i’ < |S| when we are traversing the string ‘S’.
  • The main observation is that:
    • The length of the output string is always one more than the input string. So, we are going to start the loop from ‘i’ = 0 to ‘i’ <= S.length().
    • When we encounter a D, we need numbers in decreasing order, so we just push ‘i+1’ because in the future, when we are going to empty the stack, we are having all the numbers in decreasing order beforehand.
    • When we encounter an I at the ith index of the string ‘S’, we need numbers in increasing order. Since every time we are pushing ‘i+1’ in the stack, we first push ‘i+1’, and also we have to empty the stack whenever we are facing an I. So, 2 cases are possible:
      • If S[i-1] is equal to “D”, we want numbers in the decreasing order, and the stack contains numbers in the decreasing order itself. So, just pop the element, add it into the 'ANS' string until the stack becomes empty.
      • If S[i-1] is equal to “I”, the stack only contains ‘i+1’ since the stack is already empty in the (i-1)th index because it is I. So, just add the top element in the 'ANS' string and pop that element which implies stack again become empty.
    • When we are at the last of the string, just pop the element and add it into the 'ANS' string until the stack becomes empty.
  • Combing all the observations in the code, run a loop from i=0 to ‘i’<=|S|, in each iteration:
    • Push ‘i+1’ in the stack.
    • If S[i] == ‘I’ or ‘i’ == S.length(), then pop the element, add it into the string 'ANS' till the stack becomes empty.
  • Return the string 'ANS'.
Time Complexity

O(N), where ‘N’ is the length of the string ‘S’.

 

As we are traversing the input string ‘S’ only 1 time.

Space Complexity

O(N), where ‘N' is the length of the string ‘S’.

 

As we are using a stack, which can go maximum up to ‘N’.

Code Solution
(100% EXP penalty)
Number following a pattern
Full screen
Console