[Solved] 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].

Table of Contents

Question

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:

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]

Constraints:

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

Python Solution

class Solution:
    def diStringMatch(self, S: str) -> List[int]:
        mini=0
        maxi=len(S)
        l=[]
        
        for i in S:
            if i=="I":
                l.append(mini)
                mini+=1
            else:
                l.append(maxi)
                maxi-=1
        return l+[maxi]

Leave a Reply

Your email address will not be published. Required fields are marked *