DI String Match

A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:

  • s[i] == 'I' if perm[i] < perm[i + 1], and
  • s[i] == 'D' if perm[i] > perm[i + 1].

Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.

Example 1
Inputs = "IDID"
Output[0,4,1,3,2]
The returned permutation satisfies 0 < 4 > 1 < 3 > 2, matching s = "IDID".
Example 2
Inputs = "III"
Output[0,1,2,3]
The returned permutation is strictly increasing, matching s = "III".

Constraints

  • 1 <= s.length <= 10^5
  • s[i] is either 'I' or 'D'.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate