Generate a string consisting of characters ‘a’ and ‘b’ that satisfy the given conditions

Given two integers A and B, the task is to generate and print a string str such that:

  1. str must only contain the characters ‘a’ and ‘b’.
  2. str has length A + B and the occurrence of character ‘a’ is equal to A and the occurrence of character ‘b’ is equal to B
  3. The sub-strings “aaa” or “bbb” must not occur in str.

Note that for the given values of A and B, a valid string can always be generated.

Examples:

Input: A = 1, B = 2
Output: abb
“abb”, “bab” and “bba” are all valid strings.

Input: A = 4, B = 1
Output: aabaa



Approach:

  • If occurrence(a) > occurrence(b) then append “aab”
  • If occurrence(b) > occurrence(a) then append “bba”
  • If occurrence(a) = occurrence(b) then append “ab”

Since we reduce the difference between the occurrences of ‘a’ and ‘b’ by at most 1 in each iteration so “bba” and “aab” are guaranteed not to be followed by “aab” and “bba” respectively.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

bbabbabb


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992, Ryuga



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.