 Open in App
Not now

# Maximize minority character deletions that can be done from given Binary String substring

• Difficulty Level : Medium
• Last Updated : 17 Mar, 2022

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

• Case-1: When all the 0s or 1s can be removed. When the total count of ‘0’ and ‘1′ are not same select the entire string and remove all the occurrences of the minority element.
• Case-2: When both the characters are in same number. Here choosing the entire string will not be able to remove any character. So take a substring in such a way that the count of one of the character is same as of its count in actual string and for the other it is one less. So then possible removals are (count of any character in whole string – 1).
• Case-3: When the string contains only one type of character. Then no removal is possible.

Below is the implementation of the above approach.

## C++

 `// C++ program to implement the approach` `#include ` `using` `namespace` `std;`   `// Function to find maximum number of removals` `int` `maxRem(string str)` `{` `    ``// Map to store count of zeroes and ones` `    ``map<``char``, ``int``> mp;` `    ``for` `(``auto``& value : str)` `        ``mp[value]++;`   `    ``// If the count of both characters are same` `    ``// then longest substring is the whole string` `    ``// except the last character` `    ``if` `(mp[``'0'``] == mp[``'1'``]) {` `        ``return` `(mp[``'0'``] - 1);` `    ``}`   `    ``// If the count of both characters` `    ``// are unequal so the largest substring` `    ``// is whole string and ans is` `    ``// the minimum count of a character` `    ``else` `        ``return` `min(mp[``'0'``], mp[``'1'``]);` `}`   `// Driver code` `int` `main()` `{` `    ``string str = ``"00110001000"``;`   `    ``int` `ans = maxRem(str);` `    ``cout << ans;` `    ``return` `0;` `}`

## Java

 `// Java program to implement the approach` `import` `java.util.*;` `class` `GFG{`   `  ``// Function to find maximum number of removals` `  ``static` `int` `maxRem(String str)` `  ``{` `    `  `    ``// Map to store count of zeroes and ones` `    ``HashMap mp = ``new` `HashMap();` `    ``for` `(``char` `value : str.toCharArray())` `      ``if``(mp.containsKey(value))` `        ``mp.put(value, mp.get(value)+``1``);` `    ``else` `      ``mp.put(value, ``1``);`   `    ``// If the count of both characters are same` `    ``// then longest subString is the whole String` `    ``// except the last character` `    ``if` `(mp.get(``'0'``) == mp.get(``'1'``)) {` `      ``return` `(mp.get(``'0'``) - ``1``);` `    ``}`   `    ``// If the count of both characters` `    ``// are unequal so the largest subString` `    ``// is whole String and ans is` `    ``// the minimum count of a character` `    ``else` `      ``return` `Math.min(mp.get(``'0'``), mp.get(``'1'``));` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``String str = ``"00110001000"``;`   `    ``int` `ans = maxRem(str);` `    ``System.out.print(ans);` `  ``}` `}`   `// This code is contributed by shikhasingrajput`

## Python3

 `# Python program to implement the approach` `# Function to find maximum number of removals` `def` `maxRem(s, n):`   `    ``# Map to store count of zeroes and ones` `    ``mp ``=` `{}`   `    ``for` `i ``in` `range``(``0``, n):` `        ``if``(``not` `mp.__contains__(s[i])):` `            ``mp[s[i]] ``=` `1` `        ``else``:` `            ``mp[s[i]] ``+``=` `1`   `    ``# If the count of both characters are same` `    ``# then longest substring is the whole string` `    ``# except the last character` `    ``if``(mp[``'0'``] ``=``=` `mp[``'1'``]):` `        ``return` `(mp[``'0'``] ``-` `1``)`   `    ``# If the count of both characters` `    ``# are unequal so the largest substring` `    ``# is whole string and ans is` `    ``# the minimum count of a character` `    ``else``:` `        ``return` `min``(mp[``'0'``], mp[``'1'``])`   `# Driver code`   `s ``=` `"00110001000"` `n ``=` `len``(s)` `ans ``=` `maxRem(s, n)` `print``(ans)`   `# This code is contributed by Palak Gupta`

## C#

 `// C# program to implement the approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG` `{`   `  ``// Function to find maximum number of removals` `  ``static` `int` `maxRem(``string` `str)` `  ``{`   `    ``// Map to store count of zeroes and ones` `    ``Dictionary<``char``, ``int``> mp =` `      ``new` `Dictionary<``char``, ``int``>();`   `    ``for` `(``int` `i = 0; i < str.Length; i++) {` `      ``if``(!mp.ContainsKey(str[i])) {` `        ``mp.Add(str[i], 1);` `      ``}` `      ``else` `{` `        ``mp[str[i]] = mp[str[i]] + 1;` `      ``}` `    ``}`   `    ``// If the count of both characters are same` `    ``// then longest substring is the whole string` `    ``// except the last character` `    ``if` `(mp[``'0'``] == mp[``'1'``]) {` `      ``return` `(mp[``'0'``] - 1);` `    ``}`   `    ``// If the count of both characters` `    ``// are unequal so the largest substring` `    ``// is whole string and ans is` `    ``// the minimum count of a character` `    ``else` `{` `      ``return` `Math.Min(mp[``'0'``], mp[``'1'``]);` `    ``}` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{` `    ``string` `str = ``"00110001000"``;`   `    ``int` `ans = maxRem(str);` `    ``Console.Write(ans);` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles