![]() |
VOOZH | about |
Python
Given binary string str of size, N. Select any substring from the string and remove all the occurrences of the minority character (i.e. the character having less frequency) from the substring. The task is to find out the maximum number of characters that can be removed from performing one such operation.
Note: If any substring has both '0' and '1' in the same numbers then no character can be removed.
Examples:
Input: str = "01"
Output: 0
Explanation: No character can be removed.
The substrings are "0", "1" and "01".
For "0" minority character is '1' and removing that from this substring is not possible as no '1' here.
Same for the substring "1". And substring "01" has no minority element.
The occurrences of both '1' and '0' are same in "01" substring.Input: str = "00110001000"
Output: 3
Explanation: Remove all 1s from the substring "1100010".
Approach: Following are the cases for maximum possible deletions
Below is the implementation of the above approach.
3
Time Complexity: O(N)
Auxiliary Space: O(1)