Construct Smallest Number From DI String - Problem

๐ŸŽฏ Construct the Smallest Number From DI String

You're given a pattern string consisting of 'I' (increasing) and 'D' (decreasing) characters. Your mission is to construct the lexicographically smallest number string that follows this pattern perfectly!

The Rules:

  • Use digits 1-9, each digit at most once
  • If pattern[i] == 'I', then num[i] < num[i+1] (increasing)
  • If pattern[i] == 'D', then num[i] > num[i+1] (decreasing)
  • Return the smallest possible result

Example: Pattern "IIDD" โ†’ Result "12543"
โœ… 1<2 (I), 2<5 (I), 5>4 (D), 4>3 (D)

Input & Output

example_1.py โ€” Basic Pattern
$ Input: pattern = "IIDD"
โ€บ Output: "12543"
๐Ÿ’ก Note: Start with 1, increase twice (1โ†’2โ†’5), then decrease twice (5โ†’4โ†’3). This gives us the smallest possible numbers while following the pattern.
example_2.py โ€” All Decreasing
$ Input: pattern = "DDD"
โ€บ Output: "54321"
๐Ÿ’ก Note: Since we need to decrease 4 times starting from the smallest possible number, we start with 5 and go down: 5โ†’4โ†’3โ†’2โ†’1.
example_3.py โ€” Single Character
$ Input: pattern = "I"
โ€บ Output: "12"
๐Ÿ’ก Note: Simple increase pattern: start with 1, increase to 2. This is the lexicographically smallest solution.

Visualization

Tap to expand
Construct Smallest Number AlgorithmPattern: "IDDI" โ†’ Result: "2143"2143IDDIStack Operations1. Start: Stack = [0]2. 'I': Pop 0, assign 2, push 13. 'D': Push 2 โ†’ Stack = [1,2]4. 'D': Push 3 โ†’ Stack = [1,2,3]5. 'I': Pop all, assign 1,4,3Final: "2143"Verification2 > 1 (I) โœ“1 < 4 (D) โœ“4 > 3 (D) โœ“All constraints satisfied!Greedy assignment ensures lexicographically smallest result
Understanding the Visualization
1
Scan Pattern
Read the DI pattern and identify segments
2
Use Stack
Stack holds positions waiting for number assignment
3
Handle 'I'
Pop stack and assign consecutive numbers
4
Handle 'D'
Push position onto stack (delay assignment)
5
Final Assignment
Assign remaining numbers to positions in stack
Key Takeaway
๐ŸŽฏ Key Insight: Use a stack to defer number assignment during decreasing sequences, then assign the smallest possible consecutive numbers when the sequence ends. This guarantees the lexicographically smallest valid result.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the pattern, each element pushed and popped once

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack can hold up to n elements in worst case (all decreasing)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค pattern.length โ‰ค 8
  • pattern contains only 'I' and 'D'
  • The result will have pattern.length + 1 digits
  • Each digit from 1 to 9 can be used at most once
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.3K Views
Medium Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen