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
- 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
- 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
- Minimum number of given operations required to convert a string to another string
- 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
- Check if given Binary string follows then given condition or not
- Minimum given operations required to convert a given binary string to all 1's
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum string such that every adjacent character of given string is still adjacent
- Minimum bit changes in Binary Circular array to reach a index
- Perform range sum queries on string as per given condition
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Minimum number of operations required to obtain a given Binary String
- Minimum operations required to convert all characters of a String to a given Character
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.