Generate a sequence with the given operations Last Updated : 07 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given a string S which contains only I (increase) and D (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. Examples: Input: S = "DDI" Output: [3, 2, 0, 1] Input: S = "IDID" Output: [0, 4, 1, 3, 2] Approach: If S[0] == "I", then choose 0 as the first element. Similarly, if S[0] == "D", then choose N as the first element. Now for every I operation, choose the next maximum element which hasn't been chosen before from the range [0, N], and for the D operation, choose the next minimum. Below is the implementation of the above approach: C++ //C++ Implementation of above approach #include<bits/stdc++.h> using namespace std; // function to find minimum required permutation void StringMatch(string s) { int lo=0, hi = s.length(), len=s.length(); vector<int> ans; for (int x=0;x<len;x++) { if (s[x] == 'I') { ans.push_back(lo) ; lo += 1; } else { ans.push_back(hi) ; hi -= 1; } } ans.push_back(lo) ; cout<<"["; for(int i=0;i<ans.size();i++) { cout<<ans[i]; if(i!=ans.size()-1) cout<<","; } cout<<"]"; } // Driver code int main() { string S = "IDID"; StringMatch(S); return 0; } //contributed by Arnab Kundu Java // Java Implementation of above approach import java.util.*; class GFG { // function to find minimum required permutation static void StringMatch(String s) { int lo=0, hi = s.length(), len=s.length(); Vector<Integer> ans = new Vector<>(); for (int x = 0; x < len; x++) { if (s.charAt(x) == 'I') { ans.add(lo) ; lo += 1; } else { ans.add(hi) ; hi -= 1; } } ans.add(lo) ; System.out.print("["); for(int i = 0; i < ans.size(); i++) { System.out.print(ans.get(i)); if(i != ans.size()-1) System.out.print(","); } System.out.print("]"); } // Driver code public static void main(String[] args) { String S = "IDID"; StringMatch(S); } } // This code is contributed by Rajput-Ji Python # Python Implementation of above approach # function to find minimum required permutation def StringMatch(S): lo, hi = 0, len(S) ans = [] for x in S: if x == 'I': ans.append(lo) lo += 1 else: ans.append(hi) hi -= 1 return ans + [lo] # Driver code S = "IDID" print(StringMatch(S)) C# // C# Implementation of above approach using System; using System.Collections.Generic; class GFG { // function to find minimum required permutation static void StringMatch(String s) { int lo=0, hi = s.Length, len=s.Length; List<int> ans = new List<int>(); for (int x = 0; x < len; x++) { if (s[x] == 'I') { ans.Add(lo) ; lo += 1; } else { ans.Add(hi) ; hi -= 1; } } ans.Add(lo) ; Console.Write("["); for(int i = 0; i < ans.Count; i++) { Console.Write(ans[i]); if(i != ans.Count-1) Console.Write(","); } Console.Write("]"); } // Driver code public static void Main(String[] args) { String S = "IDID"; StringMatch(S); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript implementation of above approach // function to find minimum required permutation function StringMatch(s) { var lo=0, hi = s.length, len=s.length; var ans=[]; for (var x=0;x<len;x++) { if (s[x] == 'I') { ans.push(lo) ; lo += 1; } else { ans.push(hi) ; hi -= 1; } } ans.push(lo) ; document.write("["); for(var i=0;i<ans.length;i++) { document.write(ans[i]); if(i!=ans.length -1) document.write(", "); } document.write("]"); } var S = "IDID"; StringMatch(S); // This code is contributed by SoumikMondal </script> Output[0,4,1,3,2] Time Complexity: O(n), where n is the length of the given string.Auxiliary Space: O(n), where n is the length of the given string. Comment More infoAdvertise with us Next Article Generate a sequence with the given operations S Sanjit_Prasad Follow Improve Article Tags : Strings DSA limited-range-elements programming-puzzle Practice Tags : Strings Similar Reads Increasing sequence with given GCD Given two integers n and g, the task is to generate an increasing sequence of n integers such that: The gcd of all the elements of the sequence is g.And, the sum of all the elements is the minimum among all possible sequences. Examples: Input: n = 6, g = 5 Output: 5 10 15 20 25 30 Input: n = 5, g = 3 min read Generate sequence with equal sum of adjacent integers Given an integer N, find a sequence of N integers such that the sum of all integers in the sequence is equal to the sum of any two adjacent integers in the sequence. Examples: Input: N = 4Output: 1 -1 1 -1Explanation: Total sum of the four integers is 0 and the sum of any two adjacent integers is al 8 min read Print N terms of Withoff Sequence Wythoff array is an infinite matrix of integers derived from the Fibonacci sequence. Every positive integer in the matrix occurs only once. Wythoff array: 1 2 3 5 8 13 ... 4 7 11 18 29 47 ... 6 10 16 26 42 68 ... 9 15 24 39 63 102 ... 12 20 32 52 84 136 ... 14 23 37 60 97 157 ... . . . . . . . . . . 8 min read Complete the sequence generated by a polynomial Given a sequence with some of its term, we need to calculate next K term of this sequence. It is given that sequence is generated by some polynomial, however complex that polynomial can be. Notice polynomial is an expression of the following form: P(x) = a0 + a1 x +a2 x^2 + a3 x^3 â¦â¦.. + an x^nThe g 10 min read Sequence and Series in Python Sequences and series are fundamental concepts in mathematics. A Sequence is an ordered list of numbers following a specific pattern, while a series is the sum of the elements of a sequence. This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python. Arith 5 min read Count elements remained same after applying the given operations Given an array X[]. By using the given operation, you need to return the number of elements having the same initial value except 1 and 2 after applying the given operations after infinite time. If at any indices 1 or 2 is present, Then after each second the elements, which are adjacent to them are i 15 min read Generate a sequence X from given sequence Y such that Yi = gcd(X1, X2 , ... , Xi) Given a sequence Y of size N where: Yi = gcd(X1, X2, X3, . . ., Xi ) of some sequence X. The task is to find such a sequence X if any such X is possible. Note: If X exists there can be multiple value possible for X. Generating any one is sufficient. Examples: Input: N = 2, Y = [4, 2]Output: [4, 2]Ex 7 min read Generate an array of size N according to the given rules Given a number N, the task is to create an array arr[] of size N, where the value of the element at every index i is filled according to the following rules: arr[i] = ((i - 1) - k), where k is the index of arr[i - 1] that has appeared second most recently. This rule is applied when arr[i - 1] is pre 10 min read Create a sequence whose XOR of elements is y Given two integers N and Y, the task is to generate a sequence of N distinct non-negative integers whose bitwise-XOR of all the elements of this generated sequence is equal to Y i.e. A1 ^ A2 ^ A3 ^ ..... ^ AN = Y where ^ denotes bitwise XOR. if no such sequence is possible then print -1.Examples: In 8 min read Minimum Steps to obtain N from 1 by the given operations Given an integer N, the task is to find the minimum number of operations needed to obtain the number N starting from 1. Below are the operations: Add 1 to the current number.Multiply the current number by 2.Multiply the current number by 3. Print the minimum number of operations required and the cor 15+ min read Like