Generate a sequence determined by the characters of a given string
Last Updated :
04 May, 2021
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++
#include <bits/stdc++.h>
using namespace std;
void StringPattern(string S)
{
int n = ( int )S.size();
int l = 0, r = n;
vector< int > res(n + 1, 0);
for ( int i = 0; i < n; i++) {
switch (S[i]) {
case '1' :
res[i] = l++;
break ;
case '0' :
res[i] = r--;
break ;
}
}
res[n] = l;
for ( int el : res) {
cout << el << " " ;
}
}
int main()
{
string s = "001" ;
StringPattern(s);
return 0;
}
|
Java
import java.util.*;
class GFG
{
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]+ " " );
}
}
public static void main(String[] args)
{
String s = "001" ;
StringPattern(s);
}
}
|
Python3
def StringPattern(S):
lo, hi = 0 , len (S)
ans = []
for x in S:
if x = = '1' :
ans.append(lo)
lo + = 1
else :
ans.append(hi)
hi - = 1
return ans + [lo]
if __name__ = = "__main__" :
s = '001'
print (StringPattern(s))
|
C#
using System;
class GFG
{
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] + " " );
}
}
public static void Main(String[] args)
{
String s = "001" ;
StringPattern(s);
}
}
|
Javascript
<script>
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]+ " " );
}
}
let s = "001" ;
StringPattern(s);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...