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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions
- Count number of substrings of a string consisting of same characters
- Length of the smallest sub-string consisting of maximum distinct characters
- Count pairs of strings that satisfy the given conditions
- Number of strings in two array satisfy the given conditions
- Generate an array of size K which satisfies the given conditions
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Maximum count of sub-strings of length K consisting of same characters
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Expand the string according to the given conditions
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Min flips of continuous characters to make all characters same in a string
- Minimum operations required to make the string satisfy the given condition
- Construct the Cypher string based on the given conditions
- Find the last remaining Character in the Binary String according to the given conditions
- String with k distinct characters and no same characters adjacent
- Generate all rotations of a given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.