DI String Match - Problem
DI String Match - Reconstruct a Permutation from Instructions

You're given a string s of length n containing only characters 'I' (Increase) and 'D' (Decrease). Your task is to construct a permutation of integers from [0, 1, 2, ..., n] that satisfies the given pattern.

Rules:
• If s[i] == 'I', then perm[i] < perm[i+1] (next number should be larger)
• If s[i] == 'D', then perm[i] > perm[i+1] (next number should be smaller)

Example: For s = "IDID", one valid permutation is [0,4,1,3,2]
• Position 0: 0 < 4 ✓ (I)
• Position 1: 4 > 1 ✓ (D)
• Position 2: 1 < 3 ✓ (I)
• Position 3: 3 > 2 ✓ (D)

Return any valid permutation that matches the pattern.

Input & Output

example_1.py — Basic Pattern
$ Input: s = "IDID"
Output: [0,4,1,3,2]
💡 Note: We need a permutation where positions follow I-D-I-D pattern. Using greedy approach: I→use min(0), D→use max(4), I→use min(1), D→use max(3), last→remaining(2). Verification: 0<4✓, 4>1✓, 1<3✓, 3>2✓
example_2.py — All Increasing
$ Input: s = "III"
Output: [0,1,2,3]
💡 Note: All characters are 'I' (increase), so we need strictly increasing sequence. Greedy approach always picks minimum available: 0,1,2,3. This is the natural ascending order.
example_3.py — All Decreasing
$ Input: s = "DDI"
Output: [3,2,0,1]
💡 Note: Pattern is D-D-I. Start with max for decreasing: D→3, D→2, I→0 (need room to increase), last→1. Verification: 3>2✓, 2>0✓, 0<1✓

Visualization

Tap to expand
Mountain Path Builder: DI String Match🏔️ Trail Instructions: "IDID"Available elevation markers: [0, 1, 2, 3, 4]Strategy: Keep min and max markers ready for smart placementTrail Construction Process:0Step 1: 'I' (Uphill) → Use LOW marker (0)4Step 2: 'D' (Downhill) → Use HIGH marker (4)1Step 3: 'I' (Uphill) → Use LOW marker (1)3Step 4: 'D' (Downhill) → Use HIGH marker (3)2Step 5: Place final marker (2)Final Trail Elevation Profile:0Start4↗ I1↘ D3↗ I2↘ D
Understanding the Visualization
1
Setup Trail Markers
You have elevation markers 0,1,2,3,4 and path instructions I-D-I-D
2
Smart Placement Strategy
Keep lowest and highest unused markers ready for strategic placement
3
Handle Uphill (I)
When trail goes uphill, use lowest marker to maximize future options
4
Handle Downhill (D)
When trail goes downhill, use highest marker to ensure room for drops
5
Complete the Trail
Place final marker and verify the path follows all requirements
Key Takeaway
🎯 Key Insight: By strategically choosing extreme values (minimum for increases, maximum for decreases), we guarantee that future constraints can always be satisfied, leading to an optimal O(n) solution.

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the string, constant work per character

n
2n
Linear Growth
Space Complexity
O(1)

Only using two pointer variables plus output array

n
2n
Linear Space

Constraints

  • 1 ≤ s.length ≤ 105
  • s[i] is either 'I' or 'D'
  • The permutation must contain all integers from 0 to n exactly once
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.4K Views
Medium Frequency
~12 min Avg. Time
892 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