Given two integers A and B, the task is to generate and print a string str such that:
- str must only contain the characters ‘a’ and ‘b’.
- str has length A + B and the occurrence of the character ‘a’ is equal to A and the occurrence of character ‘b’ is equal to B
- The sub-strings “aaa” or “bbb” must not occur in str.
Note: 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++
#include <bits/stdc++.h>
using namespace std;
void generateString( int A, int B)
{
string rt;
while (0 < A || 0 < B) {
if (A < B) {
if (0 < B--)
rt.push_back( 'b' );
if (0 < B--)
rt.push_back( 'b' );
if (0 < A--)
rt.push_back( 'a' );
}
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' );
}
else {
if (0 < A--)
rt.push_back( 'a' );
if (0 < B--)
rt.push_back( 'b' );
}
}
cout << rt;
}
int main()
{
int A = 2, B = 6;
generateString(A, B);
return 0;
}
|
Java
class GFG
{
static void generateString( int A, int B)
{
String rt = "" ;
while ( 0 < A || 0 < B)
{
if (A < B)
{
if ( 0 < B--)
{
rt += ( 'b' );
}
if ( 0 < B--)
{
rt += ( 'b' );
}
if ( 0 < A--)
{
rt += ( 'a' );
}
}
else if (B < A)
{
if ( 0 < A--)
{
rt += ( 'a' );
}
if ( 0 < A--)
{
rt += ( 'a' );
}
if ( 0 < B--)
{
rt += ( 'b' );
}
}
else
{
if ( 0 < A--)
{
rt += ( 'a' );
}
if ( 0 < B--)
{
rt += ( 'b' );
}
}
}
System.out.println(rt);
}
public static void main(String[] args)
{
int A = 2 , B = 6 ;
generateString(A, B);
}
}
|
Python 3
def generateString(A, B):
rt = ""
while ( 0 < A or 0 < B) :
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
elif (B < A):
if ( 0 < A):
rt + = 'a'
A - = 1
if ( 0 < A):
rt + = 'a'
A - = 1
if ( 0 < B):
rt + = 'b'
B - = 1
else :
if ( 0 < A):
rt + = 'a'
A - = 1
if ( 0 < B):
rt + = 'b'
B - = 1
print (rt)
if __name__ = = "__main__" :
A = 2
B = 6
generateString(A, B)
|
C#
using System;
class GFG
{
static void generateString( int A, int B)
{
string rt = "" ;
while (0 < A || 0 < B)
{
if (A < B)
{
if (0 < B--)
{
rt += ( 'b' );
}
if (0 < B--)
{
rt += ( 'b' );
}
if (0 < A--)
{
rt += ( 'a' );
}
}
else if (B < A)
{
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < B--)
{
rt += ( 'b' );
}
}
else
{
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < B--)
{
rt += ( 'b' );
}
}
}
Console.WriteLine(rt);
}
public static void Main()
{
int A = 2, B = 6;
generateString(A, B);
}
}
|
PHP
<?php
function generateString( $A , $B )
{
$rt = "" ;
while (0 < $A || 0 < $B )
{
if ( $A < $B )
{
if (0 < $B --)
{
$rt .= ( 'b' );
}
if (0 < $B --)
{
$rt .= ( 'b' );
}
if (0 < $A --)
{
$rt .= ( 'a' );
}
}
else if ( $B < $A )
{
if (0 < $A --)
{
$rt .= ( 'a' );
}
if (0 < $A --)
{
$rt .= ( 'a' );
}
if (0 < $B --)
{
$rt .= ( 'b' );
}
}
else
{
if (0 < $A --)
{
$rt .= ( 'a' );
}
if (0 < $B --)
{
$rt .= ( 'b' );
}
}
}
echo ( $rt );
}
$A = 2; $B = 6;
generateString( $A , $B );
?>
|
Javascript
<script>
function generateString(A,B)
{
let rt = "" ;
while (0 < A || 0 < B)
{
if (A < B)
{
if (0 < B--)
{
rt += ( 'b' );
}
if (0 < B--)
{
rt += ( 'b' );
}
if (0 < A--)
{
rt += ( 'a' );
}
}
else if (B < A)
{
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < B--)
{
rt += ( 'b' );
}
}
else
{
if (0 < A--)
{
rt += ( 'a' );
}
if (0 < B--)
{
rt += ( 'b' );
}
}
}
document.write(rt);
}
let A = 2, B = 6;
generateString(A, B);
</script>
|
Complexity Analysis:
- Time Complexity: O(Max(a,b))
- Auxiliary Space: O(Max(a,b))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Sep, 2022
Like Article
Save Article