Reorganizar una string binaria como ocurrencias alternativas de x e y

Dada una string binaria s y se dan dos enteros x e y. La tarea es organizar la string dada de tal manera que ‘0’ llegue al tiempo X, luego ‘1’ llegue al tiempo Y y así sucesivamente hasta que finalice uno de los ‘0’ o ‘1’. Luego concatene el resto de la string e imprima la string final. 
Dado: x o y no puede ser 0

Ejemplos: 

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
  1. Cuente el número de 0 y 1 en la string. 
  2. Ejecute un bucle hasta que finalice cualquiera de los alfabetos. 
    1. Primero imprima ‘0’ hasta x y disminuya la cuenta de 0. 
    2. Luego imprima ‘1’ hasta y y disminuya la cuenta de 1.  

Implementación:

C++

// C++ program to arrange given string
#include <bits/stdc++.h>
using namespace std;
 
// Function which arrange the given string
void arrangeString(string str, int x, int y)
{
    int count_0 = 0;
    int count_1 = 0;
    int len = str.length();
 
    // Counting number of 0's and 1's in the
    // given string.
    for (int i = 0; i < len; i++) {
        if (str[i] == '0')
            count_0++;
        else
            count_1++;
    }
 
    // Printing first all 0's x-times
    // and decrement count of 0's x-times
    // and do the similar task with '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--;
        }
      }
    }
}
 
// Driver Code
int main()
{
    string str = "01101101101101101000000";
    int x = 1;
    int y = 2;
    arrangeString(str, x, y);
    return 0;
}

Java

// Java program to arrange given string
import java.io.*;
 
class GFG
{
    // Function which arrange the given string
    static void arrangeString(String str, int x, int y)
    {
        int count_0 = 0;
        int count_1 = 0;
        int len = str.length();
     
        // Counting number of 0's and 1's in the
        // given string.
        for (int i = 0; i < len; i++)
        {
            if (str.charAt(i) == '0')
                count_0++;
            else
                count_1++;
        }
     
        // Printing first all 0's x-times
        // and decrement count of 0's x-times
        // and do the similar task with '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--;
                }
            }
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        String str = "01101101101101101000000";
        int x = 1;
        int y = 2;
        arrangeString(str, x, y);
 
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to arrange given string
 
# Function which arrange the given string
def arrangeString(str1,x,y):
    count_0=0
    count_1 =0
    n = len(str1)
 
    # Counting number of 0's and 1's in the
    # given string.
    for i in range(n):
        if str1[i] == '0':
            count_0 +=1
        else:
            count_1 +=1
 
    # Printing first all 0's x-times
    # and decrement count of 0's x-times
    # and do the similar task with '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
 
# Driver code
if __name__=='__main__':
    str1 = "01101101101101101000000"
    x = 1
    y = 2
    arrangeString(str1, x, y)
 
# This code is contributed by
# Shrikant13

C#

// C# program to arrange given string
using System;
 
class GFG {
     
    // Function which arrange the
    // given string
    static void arrangeString(string str,
                             int x, int y)
    {
        int count_0 = 0;
        int count_1 = 0;
        int len = str.Length;
     
        // Counting number of 0's and 1's
        // in the given string.
        for (int i = 0; i < len; i++)
        {
            if (str[i] == '0')
                count_0++;
            else
                count_1++;
        }
     
        // Printing first all 0's x-times
        // and decrement count of 0's x-times
        // and do the similar task with '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--;
                }
            }
        }
    }
     
    // Driver Code
    public static void Main ()
    {
        string str = "01101101101101101000000";
        int x = 1;
        int y = 2;
        arrangeString(str, x, y);
 
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to arrange given string
 
// Function which arrange
// the given string
function arrangeString($str, $x, $y)
{
    $count_0 = 0;
    $count_1 = 0;
    $len =strlen($str);
 
    // Counting number of 0's and
    // 1's in the given string.
    for ( $i = 0; $i < $len; $i++)
    {
        if ($str[$i] == '0')
            $count_0++;
        else
            $count_1++;
    }
 
    // Printing first all 0's x-times
    // and decrement count of 0's x-times
    // and do the similar task with '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--;
            }
        }
    }
}
 
    // Driver Code
    $str = "01101101101101101000000";
    $x = 1;
    $y = 2;
    arrangeString($str, $x, $y);
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
    // Javascript program to arrange given string
     
    // Function which arrange the
    // given string
    function arrangeString(str, x, y)
    {
        let count_0 = 0;
        let count_1 = 0;
        let len = str.length;
       
        // Counting number of 0's and 1's
        // in the given string.
        for (let i = 0; i < len; i++)
        {
            if (str[i] == '0')
                count_0++;
            else
                count_1++;
        }
       
        // Printing first all 0's x-times
        // and decrement count of 0's x-times
        // and do the similar task with '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>
Producción

01101101101101101000000

Complejidad de Tiempo: O(N 2 )
Espacio Auxiliar: O(1)

Este artículo es una contribución de Sahil Rajput . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *