![]() |
VOOZH | about |
Auxiliary Given a pattern containing only I's and D's. I for increasing and D for decreasing. Device an algorithm to print the minimum number following that pattern. Digits from 1-9 and digits canβt repeat.
Examples:
Input: D Output: 21 Input: I Output: 12 Input: DD Output: 321 Input: II Output: 123 Input: DIDI Output: 21435 Input: IIDDD Output: 126543 Input: DDIDDIID Output: 321654798
Source: Amazon Interview Question
Below are some important observations
Since digits can't repeat, there can be at most 9 digits in output.
Also, number of digits in output is one more than number of characters in input. Note that the first character of input corresponds to two digits in output.
Idea is to iterate over input array and keep track of last printed digit and maximum digit printed so far.
Steps were to follow to solve this problem:
Below is the implementation of the above idea:
1 3 2 5 4 1 2 3 2 1 1 2 3 2 1 4 3 5 1 2 6 5 4 3 3 2 1 6 5 4 7 9 8
Time Complexity: O(N^2), overall time complexity. Where, N is the length of the string.
Auxiliary Space: O(1).
This solution is suggested by Swapnil Trambake.
Alternate Solution:
Let's observe a few facts in case of a minimum number:
The idea is to iterate over the entire input array , keeping track of the minimum number (1-9) which can be placed at that position of the output.
The tricky part of course occurs when 'D' is encountered at index other than 0. In such a case we have to track the nearest 'I' to the left of 'D' and increment each number in the output vector by 1 in between 'I' and 'D'.
We cover the base case as follows:
Now we iterate the input string from index 1 till its end and:
Following is the program for the same:
1 3 2 5 4 1 2 3 2 1 1 2 3 2 1 4 3 5 1 2 6 5 4 3 3 2 1 6 5 4 7 9 8
Time Complexity: O(N2) ,here N is length of string .
Auxiliary Space: O(N) since N extra space has been taken.
This solution is suggested by Ashutosh Kumar.
Method 3
We can that when we encounter I, we got numbers in increasing order but if we encounter 'D', we want to have numbers in decreasing order. Length of the output string is always one more than the input string. So the loop is from 0 to the length of the string. We have to take numbers from 1-9 so we always push (i+1) to our stack. Then we check what is the resulting character at the specified index.So, there will be two cases which are as follows:-
Case 1: If we have encountered I or we are at the last character of input string, then pop from the stack and add it to the end of the output string until the stack gets empty.
Case 2: If we have encountered D, then we want the numbers in decreasing order. so we just push (i+1) to our stack.
1 3 2 5 4 1 2 3 2 1 1 2 3 2 1 4 3 5 1 2 6 5 4 3 3 2 1 6 5 4 7 9 8
Time Complexity: O(n)
Auxiliary Space: O(n), since n extra space has been taken.
This method is contributed by Roshni Agarwal.
Method 4 (Using two pointers)
Observation
Input : IDID Output : 13254 Input : I Output : 12 Input : DD Output : 321 Input : II Output : 123 Input : DIDI Output : 21435 Input : IIDDD Output : 126543 Input : DDIDDIID Output : 321654798
Below is the implementation of above approach:
13254 12 321 123 21435 126543 321654798
Time Complexity: O(N)
Auxiliary Space: O(N), since N extra space has been taken.
This solution is suggested by Brij Desai.
Method 5 (Start with the Smallest)
Start with the smallest number as the answer and keep shifting the digits when we encounter a D.
There is no need to traverse back for the index.
Follow the below steps,
Below is the implementation of the above approach:
13254 12 321 123 21435 126543 321654798
Time Complexity: O(N)
Auxiliary Space: O(N)
Input: "DDDD" Output: "432156" For input 1, pattern is like, D -> D -> D -> D 5 4 3 2 1 Input: "DDDII" Output: "432156" For input 2, pattern is like, D -> D -> D -> I -> I 4 3 2 1 5 6 Input: "IIDIDIII" Output: "124365789" For input 3, pattern is like, I -> I -> D -> I -> D -> I -> I -> I 1 2 4 3 6 5 7 8 9
214365 2143567 43215768 13254687 21354876 12438765
Time Complexity : O(n)
Auxiliary Space : O(1)
Method 7: (Substring Reversals)
The idea is to take the smallest number with len(s)+1 and perform reversals for every substring containing only 'D'.
Follow below steps to solve the problem:
1. Create the smallest possible number of length len(s)+1.
2. Traverse the string (say i).
3. Find the first and last occurrence of 'D' for every substring containing only 'D'.
4. Reverse every such substring and reinitialize first and last occurrence.
321654798
Time Complexity: O(n)
Auxiliary Space: O(1)