Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
Last Updated :
16 Sep, 2022
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++
// 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>
Complexity Analysis:
- Time Complexity: O(Max(a,b))
- Auxiliary Space: O(Max(a,b))
Similar Reads
Generate a String from given Strings P and Q based on the given conditions Given two strings P and Q, the task is to generate a string S satisfying the following conditions: Find S such that P is rearranged and Q is a substring in it.All the characters before Q in S should be smaller than or equal to the first character in Q and in lexicographic order.The rest of the chara
7 min read
Generate all permutations of a string that follow given constraints Given a string, generate all permutations of it that do not contain 'B' after 'A', i.e., the string should not contain "AB" as a substring. Examples: Input : str = "ABC" Output : ACB, BAC, BCA, CBA Out of 6 permutations of "ABC", 4 follow the given constraint and 2 ("ABC" and "CAB") do not follow. I
11 min read
Check whether given string can be generated after concatenating given strings Given three strings str, A and B. The task is to check whether str = A + B or str = B + A where + denotes concatenation. Examples: Input: str = "GeeksforGeeks", A = "Geeksfo", B = "rGeeks" Output: Yes str = A + B = "Geeksfo" + "rGeeks" = "GeeksforGeeks"Input: str = "Delhicapitals", B = "Delmi", C =
11 min read
Find smallest string with whose characters all given Strings can be generated Given an array of strings arr[]. The task is to generate the string which contains all the characters of all the strings present in array and smallest in size. There can be many such possible strings and any one is acceptable. Examples: Input: arr[] = {"your", "you", "or", "yo"}Output: ruyoExplanati
5 min read
Generate the String of length N according to given conditions Given two integers N and M. Then your task is to output the string let's say S of length N by following given conditions: S must be formed by only the first M smaller case letters of the alphabetical series.The length of LPS (Longest palindromic Substring) in S should be the minimum possible.Example
10 min read