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 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 that for the given values of A and B, a valid string can always be generated.
Input: A = 1, B = 2
“abb”, “bab” and “bba” are all valid strings.
Input: A = 4, B = 1
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Generate a String from given Strings P and Q based on the given conditions
- Count pairs of strings that satisfy the given conditions
- Number of strings in two array satisfy the given conditions
- Generate a string from an array of alphanumeric strings based on given conditions
- Length of the smallest sub-string consisting of maximum distinct characters
- Count number of substrings of a string consisting of same characters
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Generate an array of size K which satisfies the given conditions
- Sum of indices of Characters removed to obtain an Empty String based on given conditions
- Maximum count of sub-strings of length K consisting of same characters
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Smallest String consisting of a String S exactly K times as a Substring
- Minimum operations required to make the string satisfy the given condition
- Different Ways to Generate String by using Characters and Numbers in Java
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Change the given string according to the given conditions
- Count minimum character replacements required such that given string satisfies the given conditions
- Choose X elements from A and Y elements from B which satisfy the given condition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.