Form Minimum Number From a Given Sequence

Given a pattern containing only Is and Ds, I for increasing and D for decreasing, devise an algorithm to print the minimum number following that pattern. Digits are 1..9 and can’t repeat.

Examples:

"D" should return 21.

"I" should return 12.

"DD" should return 321.

"II" should return 123.

"DIDI"should return 21435.

Contributed by Berkan Teber