Generate a sequence determined by the characters of a given string
Given a binary string S of length N (1 ≤ N ≤ 105), the task is to print a permutation A consisting of integers 0 to N satisfying the following conditions:
- If S[i] = 1, then A[i] < A[i + 1].
- If S[i] = 0, then A[i] > A[i + 1].
Examples:
Input: S = “001”
Output: [3, 2, 0, 1]
Explanation:
S[0] = 0 and A[0] (= 3) > A[1] (= 2)
S[1] = 0 and A[1] (= 2) > A[2] (= 0)
S[2] = 1 and A[2] (= 0) > A[3] (= 1)
Input: S = “1010”
Output: [0, 4, 1, 3, 2]
Approach: Follow the steps below to solve the problem:
- Assign the lowest number possible if the character encountered in the string is ‘1’.
- Assign the highest number possible if the character encountered in the string is ‘0’.
- Set two pointers lo (= 0), storing the current lowest possible, and hi (= N), storing the current highest possible.
- Traverse the string and append the values to the resultant array, say res, from the range using the two pointers accordingly.
- After complete traversal of the string, only one value is remaining to be appended. Append lo to the last index.
- Print the array res as the required result.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print the // required sequence void StringPattern(string S) { // Length of the string int n = ( int )S.size(); // Pointers to store lowest and // highest possible values int l = 0, r = n; // Stores the required answer vector< int > res(n + 1, 0); for ( int i = 0; i < n; i++) { switch (S[i]) { // If current character is '1' case '1' : // Assign smallest // possible value res[i] = l++; break ; // If current character is '0' case '0' : // Assign largest // possible value res[i] = r--; break ; } } // At this point, l == r , // only one value is not assigned yet. res[n] = l; for ( int el : res) { cout << el << " " ; } } // Driver Code int main() { string s = "001" ; StringPattern(s); return 0; } |
Java
// Java Implementation of above approach import java.util.*; class GFG { // function to find minimum required permutation static void StringPattern(String s) { int n = s.length(); int l = 0 , r = n; int [] res = new int [n + 1 ]; for ( int x = 0 ; x < n; x++) { if (s.charAt(x) == '1' ) { res[x] = l++; } else if (s.charAt(x) == '0' ) { res[x] = r--; } } res[n] = l; for ( int i = 0 ; i < n+ 1 ; i++) { System.out.print(res[i]+ " " ); } } // Driver code public static void main(String[] args) { String s = "001" ; StringPattern(s); } } // This code is contributed by mohit kumar 29. |
Python3
# Python program to implement # the above approach # Function to print the # required sequence def StringPattern(S): # Stores the lowest, highest # possible values that can be # appended, size of the string lo, hi = 0 , len (S) # Stores the required result ans = [] # Traverse the string for x in S: # If current character is '1' if x = = '1' : # Append lowest # possible value ans.append(lo) lo + = 1 # If current character is '0' else : # Append highest # possible value ans.append(hi) hi - = 1 return ans + [lo] # Driver Code if __name__ = = "__main__" : # Input s = '001' print (StringPattern(s)) |
C#
// C# Implementation of above approach using System; class GFG { // function to find minimum required permutation static void StringPattern(String s) { int n = s.Length; int l = 0, r = n; int [] res = new int [n + 1]; for ( int x = 0; x < n; x++) { if (s[x] == '1' ) { res[x] = l++; } else if (s[x] == '0' ) { res[x] = r--; } } res[n] = l; for ( int i = 0; i < n + 1; i++) { Console.Write(res[i] + " " ); } } // Driver code public static void Main(String[] args) { String s = "001" ; StringPattern(s); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // Javascript program of // the above approach // function to find minimum // required permutation function StringPattern(s) { let n = s.length; let l = 0, r = n; let res = new Array(n+1).fill(0); for (let x = 0; x < n; x++) { if (s[x] == '1' ) { res[x] = l++; } else if (s[x] == '0' ) { res[x] = r--; } } res[n] = l; for (let i = 0; i < n+1; i++) { document.write(res[i]+ " " ); } } // Driver Code let s = "001" ; StringPattern(s); </script> |
Output:
3 2 0 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...