Number following the pattern
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't repeat.
Input: IIDDIDD Output: 12543876
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'.
Example with explanation:need an explanation for this answer? contact us directly to get an explanation for this answer