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 email@example.com. 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.
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.
- 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
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Number of flips to make binary string alternate | Set 1
- Minimum number of characters to be removed to make a binary string alternate
- Check if a binary string has two consecutive occurrences of one everywhere
- Rearrange given string to maximize the occurrence of string t
- Count occurrences of a string that can be constructed from another given string
- Alternate vowel and consonant string
- Lexicographically first alternate vowel and consonant string
- Occurrences of a pattern in binary representation of a number
- Rearrange a string so that all same characters become d distance away
- Rearrange a string so that all same characters become atleast d distance away
- Rearrange characters in a string such that no two adjacent are same
- Rearrange a string in sorted order followed by the integer sum
- Rearrange the string to maximize the number of palindromic substrings
- Rearrange a string in the form of integer sum followed by the minimized character
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange the given string such that all prime multiple indexes have same character