Genere una string que consista en los caracteres ‘a’ y ‘b’ que satisfagan las condiciones dadas

Dados dos enteros A y B , la tarea es generar e imprimir una string str tal que: 
 

  1. str solo debe contener los caracteres ‘a’ y ‘b’ .
  2. str tiene una longitud A + B y la aparición del carácter ‘a’ es igual a A y la aparición del carácter ‘b’ es igual a B
  3. Las substrings «aaa» o «bbb» no deben aparecer en str .

Tenga en cuenta que para los valores dados de A y B , siempre se puede generar una string válida.
Ejemplos: 
 

Entrada: A = 1, B = 2 
Salida: abb 
“abb”, “bab” y “bba” son todas strings válidas.
Entrada: A = 4, B = 1 
Salida: aabaa 
 

Acercarse: 
 

  • Si ocurrencia (a)> ocurrencia (b) , agregue «aab»
  • Si ocurrencia (b)> ocurrencia (a) , agregue «bba»
  • Si ocurrencia (a) = ocurrencia (b) , agregue «ab»

Dado que reducimos la diferencia entre las apariciones de ‘a’ y ‘b’ en 1 como máximo en cada iteración, se garantiza que «bba « y » aab» no vayan seguidos de «aab» y «bba», respectivamente.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate and print the required string
void generateString(int A, int B)
{
    string rt;
    while (0 < A || 0 < B) {
 
        // More 'b', append "bba"
        if (A < B) {
            if (0 < B--)
                rt.push_back('b');
            if (0 < B--)
                rt.push_back('b');
            if (0 < A--)
                rt.push_back('a');
        }
 
        // More 'a', append "aab"
        else if (B < A) {
            if (0 < A--)
                rt.push_back('a');
            if (0 < A--)
                rt.push_back('a');
            if (0 < B--)
                rt.push_back('b');
        }
 
        // Equal number of 'a' and 'b'
        // append "ab"
        else {
            if (0 < A--)
                rt.push_back('a');
            if (0 < B--)
                rt.push_back('b');
        }
    }
    cout << rt;
}
 
// Driver code
int main()
{
    int A = 2, B = 6;
    generateString(A, B);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
 
    // Function to generate and
    // print the required string
    static void generateString(int A, int B)
    {
        String rt = "";
        while (0 < A || 0 < B)
        {
 
            // More 'b', append "bba"
            if (A < B)
            {
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
            }
             
            // More 'a', append "aab"
            else if (B < A)
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
             
            // Equal number of 'a' and 'b'
            // append "ab"
            else
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
        }
        System.out.println(rt);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 2, B = 6;
        generateString(A, B);
    }
}
 
// This code is contributed
// by PrinciRaj1992

Python 3

# Python 3 implementation of the approach
 
# Function to generate and print
# the required string
def generateString(A, B):
 
    rt = ""
    while (0 < A or 0 < B) :
 
        # More 'b', append "bba"
        if (A < B) :
            if (0 < B):
                rt = rt +'b'
                B -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
            if (0 < A):
                rt += 'a'
                A -= 1
 
        # More 'a', append "aab"
        elif (B < A):
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
 
        # Equal number of 'a' and 'b'
        # append "ab"
        else :
            if (0 < A):
                rt += 'a'
                A -= 1
            if (0 < B):
                rt += 'b'
                B -= 1
    print(rt)
 
# Driver code
if __name__ == "__main__":
     
    A = 2
    B = 6
    generateString(A, B)
 
# This code is contributed by ita_c

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to generate and
    // print the required string
    static void generateString(int A, int B)
    {
        string rt = "";
        while (0 < A || 0 < B)
        {
 
            // More 'b', append "bba"
            if (A < B)
            {
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
            }
             
            // More 'a', append "aab"
            else if (B < A)
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
             
            // Equal number of 'a' and 'b'
            // append "ab"
            else
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
        }
        Console.WriteLine(rt);
    }
 
    // Driver code
    public static void Main()
    {
        int A = 2, B = 6;
        generateString(A, B);
    }
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP implementation of the approach
 
// Function to generate and
// print the required string
function generateString($A, $B)
{
    $rt = "";
    while (0 < $A || 0 < $B)
    {
 
        // More 'b', append "bba"
        if ($A < $B)
        {
            if (0 < $B--)
            {
                $rt .= ('b');
            }
            if (0 < $B--)
            {
                $rt .= ('b');
            }
            if (0 < $A--)
            {
                $rt .= ('a');
            }
        }
         
        // More 'a', append "aab"
        else if ($B < $A)
        {
            if (0 < $A--)
            {
                $rt .= ('a');
            }
            if (0 < $A--)
            {
                $rt .= ('a');
            }
            if (0 < $B--)
            {
                $rt .= ('b');
            }
        }
         
        // Equal number of 'a' and 'b'
        // append "ab"
        else
        {
            if (0 < $A--)
            {
                $rt .= ('a');
            }
            if (0 < $B--)
            {
                $rt .= ('b');
            }
        }
    }
    echo($rt);
}
 
// Driver code
$A = 2; $B = 6;
generateString($A, $B);
 
// This code is contributed
// by Code Mech
?>

Javascript

<script>
// Javascript implementation of the approach
 
 // Function to generate and
    // print the required string
function generateString(A,B)
{
    let rt = "";
        while (0 < A || 0 < B)
        {
   
            // More 'b', append "bba"
            if (A < B)
            {
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
            }
               
            // More 'a', append "aab"
            else if (B < A)
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
               
            // Equal number of 'a' and 'b'
            // append "ab"
            else
            {
                if (0 < A--)
                {
                    rt += ('a');
                }
                if (0 < B--)
                {
                    rt += ('b');
                }
            }
        }
        document.write(rt);
}
 
// Driver code
let A = 2, B = 6;
generateString(A, B);
 
 
// This code is contributed by avanitrachhadiya2155
</script>
Producción: 

bbabbabb

 

Publicación traducida automáticamente

Artículo escrito por Sumit bangar 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 *