Given a string which contains only (increase) and (decrease). The task is to return any permutation of integers [0, 1, …, N] where N ≤ Length of S such that for all i = 0, …, N-1:
- If S[i] == “D”, then A[i] > A[i+1]
- If S[i] == “I”, then A[i] < A[i+1].
Note that output must contain distinct elements.
Input: S = “DDI”
Output: [3, 2, 0, 1]
Input: S = “IDID”
Output: [0, 4, 1, 3, 2]
Approach: If S == “I”, then choose as the first element. Similarly, if S == “D”, then choose as the first element. Now for every operation, choose the next maximum element which hasn’t been chosen before from the range [0, N] and for the operation, choose the next minimum.
Below is the implementation of the above approach:
[0, 4, 1, 3, 2]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Convert an unbalanced bracket sequence to a balanced sequence
- Generate Passwords of given length
- Generate all the binary number from 0 to n
- Generate all rotations of a given string
- Generate n-bit Gray Codes
- Generate all possible permutations of a Number divisible by N
- Generate all binary strings from given pattern
- Generate all binary strings without consecutive 1's
- Generate random String of given size in Java
- Program to generate all possible valid IP addresses from given string | Set 2
- Generate number with given operation and check if it is palindrome
- Generate a string with maximum possible alphabets with odd frequencies
- Generate all permutations of a string that follow given constraints
- Generate a String of having N*N distinct non-palindromic Substrings
- Generate permutations with only adjacent swaps allowed
- Program to generate all possible valid IP addresses from given string
- Minimum flips required to generate continuous substrings of 0’s and 1’s
- Generate all possible strings such that char at index i is either str1[i] or str2[i]
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.