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. Digits are from 1-9 and digits can not repeat
belongs to collection: Interview C++ coding problems/challenges | stack
All Answers
total answers (1)

C++ programming
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 FUNCTIONC++ Implementation
Output
Example with explanation:
need an explanation for this answer? contact us directly to get an explanation for this answer