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.
- 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 changes required to make first string substring of second string
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Minimum operations required to make the string satisfy the given condition
- Find minimum changes required in an array for it to contain k distinct elements
- Minimum changes required to make all element in an array equal
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum number of changes such that elements are first Negative and then Positive
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum changes to a string to make all substrings distinct
- Generate an array of size K which satisfies the given conditions
- Find an array of size N that satisfies the given conditions
- Check if given Binary string follows then given condition or not
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Minimum number of given operations required to convert a string to another string
- Minimum bit changes in Binary Circular array to reach a index
- Minimum given operations required to convert a given binary string to all 1's
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.