Construct Smallest Number From DI String

MediumStringMath

Description

You are given a string pattern of I's and D's: I means the next digit is greater (increase), D means the next digit is smaller (decrease). Return the lexicographically smallest possible string of digits 1-9 without repeating.

Examples

Input:pattern = "IIIDIDDD"
Output:"123549876"
Explanation:

Smallest valid arrangement.

Input:pattern = "a"
Output:"123549876"
Explanation:

Edge case with a single character.

Input:pattern = "DDD"
Output:4321
Explanation:

With a fully descending pattern, the smallest valid number is 4321.

Constraints

  • 1 ≤ pattern.length ≤ 8

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!