top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Find Permutation - Interviewbit Solution

Sep 1, 2020
1 min read

Updated: Sep 8, 2020


Problem Description:

Given a positive integer n and a string s consisting only of letters D or I, you have to find any permutation of first n positive integer that satisfy the given input string.

D means the next number is smaller, while I means the next number is greater.


Notes

  • Length of given string s will always equal to n - 1

  • Your solution should run in linear time and space.


Example :

Input 1:
n = 3
s = ID

Return: [1, 3, 2]

Solution:


1 Comment


RioPrice
Nov 10

https://skrillcasinosite.com came to mind when I read that the solution should run in linear time and space, since that constraint is what makes the stack-based approach so satisfying compared to brute force. The trick of reversing each run of D's between the I's is genuinely elegant, and I appreciated how the example with n = 3 and s = ID makes it click immediately. I've seen this problem before but never thought about it in terms of segment reversal, which feels much cleaner than building the permutation one position at a time.

Like
bottom of page