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