Given a pattern containing only I's and D's. 'I' stands for increasing and 'D' for decreasing. Devise an algorithm to print the minimum number following that pattern
All Answers
need an explanation for this answer? contact us directly to get an explanation for this answer
test answer for test notification
need an explanation for this answer? contact us directly to get an explanation for this answertotal answers (2)

C++ programming
Solution
The pattern & number to be generated
Hence, maximum string length possible is 8, since we can construct only with different digits (1-9)
"II" → 123
"DD" → 321
The problem can be used with help of stack. The concept is to create stack with consecutive number same as depth of a local contiguous sequence of 'D'.
Prerequisite:
Function findMinFromPattern(string s) 1. Declare a stack st; 2. FOR i=0 : pattern length EnQueue (st, i+1); //push i+1 at first, i+1 becuase i is 0-indexed IF (entire pattern is processed || s[i] =='I') While(stack is not empty){ Pop and print End While END IF END FOR END FUNCTIONOutput
Example with explanation:
need an explanation for this answer? contact us directly to get an explanation for this answer