Given a binary string str, the task is to maximize the count of 0s in left substring and 1s in right substring by splitting given Binary string at any index. Print the sum of such 0s and 1s in the end.
Input: str = “0011110011”
If string is splitted at index 2, then Left substring = “00” and Right substring = “11110011”.
Therefore the sum of the count of 0s in left substring and 1s in right substring is 2 + 6 = 8.
Input: str = “0001101111011”
If string is splitted at index 3, then Left substring is “000” and Right substring is “1101111011”.
Therefore the sum of the count of 0s in left substring and 1s in right substring is 3 + 8 = 11.
- Count the number of ones in the given binary string str (say totalOnes).
- Traverse the given string and keep the counts of 0s (say zero) and 1s (say one).
- During the string traversal update the maximum sum as:
maxSum = max(maxSum, zero + (totalOnes – one))
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
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 number of set bits count in a K-size substring of a Binary String
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- String formed with middle character of every right substring followed by left sequentially
- Maximize count of set bits in a root to leaf path in a binary tree
- Minimum steps to remove substring 010 from a binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum flips required in a binary string such that all K-size substring contains 1
- Decode a string recursively encoded as count followed by substring
- Splitting a Numeric String
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count of 1-bit and 2-bit characters in the given binary string
- Count of substrings of a binary string containing K ones
- Count of non-overlapping sub-strings "101" and "010" in the given binary string
- Count of cyclic permutations having XOR with other binary string as 0
- Count of substrings of a given Binary string with all characters same
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.