Given a binary string str. In a single operation, we can change any ‘1’ to ‘0’ or any ‘0’ to ‘1’. The task is to make minimum number of changes in the string such that if we take any prefix of the string, the number of 1’s should be greater than or equal number of 0’s.
Input: str = “10001”
We can change str from ‘0’ to ‘1’.
Input: str = “0000”
Approach: The problem can be solved greedily. The first character of the string has to be 1. Then for the rest of the string we traverse through the string character by character and check if the required condition is fulfilled or not, if not then we increase the count of changes required.
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 operations required to make the string satisfy the given condition
- Check if the string satisfies the given condition
- Find the lexicographically smallest string which satisfies the given condition
- Maximum size of sub-array that satisfies the given condition
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
- Minimum flips required to keep all 1s together in a Binary string
- Minimum number of subsequences required to convert one string to another
- Minimum number of operations required to sum to binary string S
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum swaps required to convert one binary string to another
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum swaps required to make a binary string alternating
- Minimum number of swaps required to make the string K periodic
- Minimum number of operations required to obtain a given Binary 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.