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:
// Return the required count
// Driver code
$str = “0000”;
$n = strlen($str);
echo minChanges($str, $n);
// This code is contributed by mits
- 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 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
- Minimum rotations required to get the same String | Set-2
- Minimum swaps required to convert one binary string to another
- Minimum number of operations required to sum to binary string S
- Minimum swaps required to make a binary string alternating
- Minimum cuts required to convert a palindromic string to a different palindromic string
- 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 number of operations required to reduce N to 1
- Minimum boxes required to carry all gifts
- Minimum number of days required to complete the work
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.