Rearrange a binary string as alternate x and y occurrences
Given a binary string s and two integers x and y are given. Task is to arrange the given string in such a way so that ‘0’ comes X-time then ‘1’ comes Y-time and so on until one of the ‘0’ or ‘1’ is finished. Then concatenate rest of the string and print the final string.
Given : x or y can not be 0
Examples:
Input : s = "0011"
x = 1
y = 1
Output : 0101
x is 1 and y is 1. So first we print
'0' one time the '1' one time and
then we print '0', after printing '0',
all 0's are vanished from the given
string so we concatenate rest of the
string which is '1'.
Input : s = '1011011'
x = 1
y = 1
Output : 0101111
- Count number of 0’s and 1’s in the string.
- Run a loop until either one of the alphabets is finished.
- First print ‘0’ upto x and decrement count of 0.
- Then print ‘1’ upto y and decrement count of 1.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void arrangeString(string str, int x, int y)
{
int count_0 = 0;
int count_1 = 0;
int len = str.length();
for ( int i = 0; i < len; i++) {
if (str[i] == '0' )
count_0++;
else
count_1++;
}
while (count_0 > 0 || count_1 > 0) {
for ( int j = 0; j < x && count_0 > 0; j++) {
if (count_0 > 0) {
cout << "0" ;
count_0--;
}
}
for ( int j = 0; j < y && count_1 > 0; j++) {
if (count_1 > 0) {
cout << "1" ;
count_1--;
}
}
}
}
int main()
{
string str = "01101101101101101000000" ;
int x = 1;
int y = 2;
arrangeString(str, x, y);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void arrangeString(String str, int x, int y)
{
int count_0 = 0 ;
int count_1 = 0 ;
int len = str.length();
for ( int i = 0 ; i < len; i++)
{
if (str.charAt(i) == '0' )
count_0++;
else
count_1++;
}
while (count_0 > 0 || count_1 > 0 )
{
for ( int j = 0 ; j < x && count_0 > 0 ; j++)
{
if (count_0 > 0 )
{
System.out.print ( "0" );
count_0--;
}
}
for ( int j = 0 ; j < y && count_1 > 0 ; j++)
{
if (count_1 > 0 )
{
System.out.print( "1" );
count_1--;
}
}
}
}
public static void main (String[] args)
{
String str = "01101101101101101000000" ;
int x = 1 ;
int y = 2 ;
arrangeString(str, x, y);
}
}
|
Python3
def arrangeString(str1,x,y):
count_0 = 0
count_1 = 0
n = len (str1)
for i in range (n):
if str1[i] = = '0' :
count_0 + = 1
else :
count_1 + = 1
while count_0> 0 or count_1> 0 :
for i in range ( 0 ,x):
if count_0> 0 :
print ( "0" ,end = "")
count_0 - = 1
for j in range ( 0 ,y):
if count_1> 0 :
print ( "1" ,end = "")
count_1 - = 1
if __name__ = = '__main__' :
str1 = "01101101101101101000000"
x = 1
y = 2
arrangeString(str1, x, y)
|
C#
using System;
class GFG {
static void arrangeString( string str,
int x, int y)
{
int count_0 = 0;
int count_1 = 0;
int len = str.Length;
for ( int i = 0; i < len; i++)
{
if (str[i] == '0' )
count_0++;
else
count_1++;
}
while (count_0 > 0 || count_1 > 0)
{
for ( int j = 0; j < x &&
count_0 > 0; j++)
{
if (count_0 > 0)
{
Console.Write( "0" );
count_0--;
}
}
for ( int j = 0; j < y &&
count_1 > 0; j++)
{
if (count_1 > 0)
{
Console.Write( "1" );
count_1--;
}
}
}
}
public static void Main ()
{
string str = "01101101101101101000000" ;
int x = 1;
int y = 2;
arrangeString(str, x, y);
}
}
|
PHP
<?php
function arrangeString( $str , $x , $y )
{
$count_0 = 0;
$count_1 = 0;
$len = strlen ( $str );
for ( $i = 0; $i < $len ; $i ++)
{
if ( $str [ $i ] == '0' )
$count_0 ++;
else
$count_1 ++;
}
while ( $count_0 > 0 || $count_1 > 0)
{
for ( $j = 0; $j < $x &&
$count_0 > 0; $j ++)
{
if ( $count_0 > 0)
{
echo "0" ;
$count_0 --;
}
}
for ( $j = 0; $j < $y &&
$count_1 > 0; $j ++)
{
if ( $count_1 > 0)
{
echo "1" ;
$count_1 --;
}
}
}
}
$str = "01101101101101101000000" ;
$x = 1;
$y = 2;
arrangeString( $str , $x , $y );
?>
|
Javascript
<script>
function arrangeString(str, x, y)
{
let count_0 = 0;
let count_1 = 0;
let len = str.length;
for (let i = 0; i < len; i++)
{
if (str[i] == '0' )
count_0++;
else
count_1++;
}
while (count_0 > 0 || count_1 > 0)
{
for (let j = 0; j < x && count_0 > 0; j++)
{
if (count_0 > 0)
{
document.write( "0" );
count_0--;
}
}
for (let j = 0; j < y && count_1 > 0; j++)
{
if (count_1 > 0)
{
document.write( "1" );
count_1--;
}
}
}
}
let str = "01101101101101101000000" ;
let x = 1;
let y = 2;
arrangeString(str, x, y);
</script>
|
Output
01101101101101101000000
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
20 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...