Given a binary string s and two integers x and y are given. Task is to arrange the given string in such a way so that ‘0’ comes X-time then ‘1’ comes Y-time and so on until one of the ‘0’ or ‘1’ is finished. Then concatenate rest of the string and print the final string.
Given : x or y can not be 0
Input : s = "0011" x = 1 y = 1 Output : 0101 x is 1 and y is 1. So first we print '0' one time the '1' one time and then we print '0', after printing '0', all 0's are vanished from the given string so we concatenate rest of the string which is '1'. Input : s = '1011011' x = 1 y = 1 Output : 0101111
1. Count number of 0’s and 1’s in the string.
2. Run a loop until either one of the alphabets is finished.
2.1. First print ‘0’ upto x and decrement count of 0.
2.2. Then print ‘1’ upto y and decrement count of 1.
This article is contributed by Sahil Rajput. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Print the string by ignoring alternate occurrences of any character
- Check if a binary string has two consecutive occurrences of one everywhere
- Number of flips to make binary string alternate | Set 1
- Minimum number of characters to be removed to make a binary string alternate
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Rearrange given string to maximize the occurrence of string t
- Count occurrences of a string that can be constructed from another given string
- Occurrences of a pattern in binary representation of a number
- Rearrange a string so that all same characters become d distance away
- Rearrange characters in a string such that no two adjacent are same
- Replace all occurrences of pi with 3.14 in a given string
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange a string in sorted order followed by the integer sum