#942

DI String Match

newbie · 205 · lc easy +16 · verified · 80.9% accepted · 2,606 likes · top 92%

Description

A string s of length n encodes ordering constraints for a permutation of 0..n: each 'I' requires the value to increase at that position and each 'D' requires a decrease. Given s, reconstruct and return any valid permutation that satisfies every constraint.

Example 1:

Input: s = "IDID"
Output: [0,4,1,3,2]

Example 2:

Input: s = "III"
Output: [0,1,2,3]

Example 3:

Input: s = "DDI"
Output: [3,2,0,1]

Code

1
2
3