Maximize minority character deletions that can be done from given Binary String substring
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++
#include <bits/stdc++.h>
using namespace std;
int maxRem(string str)
{
map< char , int > mp;
for ( auto & value : str)
mp[value]++;
if (mp[ '0' ] == mp[ '1' ]) {
return (mp[ '0' ] - 1);
}
else
return min(mp[ '0' ], mp[ '1' ]);
}
int main()
{
string str = "00110001000" ;
int ans = maxRem(str);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxRem(String str)
{
HashMap<Character,Integer> mp = new HashMap<Character,Integer>();
for ( char value : str.toCharArray())
if (mp.containsKey(value))
mp.put(value, mp.get(value)+ 1 );
else
mp.put(value, 1 );
if (mp.get( '0' ) == mp.get( '1' )) {
return (mp.get( '0' ) - 1 );
}
else
return Math.min(mp.get( '0' ), mp.get( '1' ));
}
public static void main(String[] args)
{
String str = "00110001000" ;
int ans = maxRem(str);
System.out.print(ans);
}
}
|
Python3
def maxRem(s, n):
mp = {}
for i in range ( 0 , n):
if ( not mp.__contains__(s[i])):
mp[s[i]] = 1
else :
mp[s[i]] + = 1
if (mp[ '0' ] = = mp[ '1' ]):
return (mp[ '0' ] - 1 )
else :
return min (mp[ '0' ], mp[ '1' ])
s = "00110001000"
n = len (s)
ans = maxRem(s, n)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int maxRem( string str)
{
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 (mp[ '0' ] == mp[ '1' ]) {
return (mp[ '0' ] - 1);
}
else {
return Math.Min(mp[ '0' ], mp[ '1' ]);
}
}
public static void Main()
{
string str = "00110001000" ;
int ans = maxRem(str);
Console.Write(ans);
}
}
|
Javascript
<script>
function maxRem(str)
{
let mp = new Map();
for (let i = 0; i < str.length; i++) {
if (mp.has(str[i])) {
mp.set(str[i], mp.get(str[i]) + 1)
}
else {
mp.set(str[i], 1)
}
}
if (mp.get( '0' ) == mp.get( '1' )) {
return (mp.get( '0' ) - 1);
}
else
return Math.min(mp.get( '0' ), mp.get( '1' ));
}
let str = "00110001000" ;
let ans = maxRem(str);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
17 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...