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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum difference of zeros and ones in binary string
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Maximum contiguous 1 possible in a binary string after k rotations
- Find the maximum possible Binary Number from given string
- Maximum Consecutive Zeroes in Concatenated Binary String
- Arrange a binary string to get maximum value within a range of indices
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Maximum splits in binary string such that each substring is divisible by given odd number
- Count binary strings with twice zeros in first half
- Maximum number of strings that can be formed with given zeros and ones
- Maximum number of set bits count in a K-size substring of a Binary String
- Maximum number of trailing zeros in the product of the subsets of size k
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- String obtained by reversing and complementing a Binary string K times
- Sum of all LCP of maximum length by selecting any two Strings at a time
- Longest sub string of 0's in a binary string which is repeated K times
- Print string after removing all (“10” or “01”) from the binary string
- Python | Check if a given string is binary string or not
- Largest sub-string of a binary string divisible by 2
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.