Given a binary string of 0s and 1s. The task is to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string. That is maximize ( number of 0s – number of 1s ) for any sub-string in the given binary string.
Input : S = "11000010001" Output : 6 From index 2 to index 9, there are 7 0s and 1 1s, so number of 0s - number of 1s is 6. Input : S = "1111" Output : -1
We have discussed Dynamic Programing approach in below post :
Maximum difference of zeros and ones in binary string | Set 1.
In the post we seen an efficient method that work in O(n) time and in O(1) extra space. Idea behind that if we convert all zeros into 1 and all ones into -1.now our problem reduces to find out the maximum sum sub_array Using Kadane’s Algorithm.
Input : S = "11000010001" After converting '0' into 1 and '1' into -1 our S Look Like S = -1 -1 1 1 1 1 -1 1 1 1 -1 Now we have to find out Maximum Sum sub_array that is : 6 is that case Output : 6
Below is the implementation of above idea.
Time Complexity : O(n)
Space complexity : O(1)
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.
- Maximum difference of zeros and ones in binary string
- Maximum number of strings that can be formed with given zeros and ones
- Count of substrings of a binary string containing K ones
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count binary strings with twice zeros in first half
- Check whether two strings can be made equal by copying their characters with the adjacent ones
- Remove leading zeros from a Number given as a string
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Maximum time such that absolute difference between hour and minute lies in given range
- Find the time which is palindromic and comes after the given time
- Maximum number of set bits count in a K-size substring of a Binary String
- Python | Remove leading zeros from an IP address
- Removing elements between the two zeros
- String obtained by reversing and complementing a Binary string K times
- Check if binary representations of 0 to N are present as substrings in given binary string
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Maximum contiguous 1 possible in a binary string after k rotations
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.