最大化可以从给定的二进制字符串子字符串中完成的少数字符删除
Python
给定大小为N的二进制字符串str 。从字符串中选择任意一个子字符串,并从子字符串中删除所有出现的少数字符(即频率较低的字符)。任务是找出可以从执行一项此类操作中删除的最大字符数。
注意:如果任何子字符串在相同的数字中同时包含“0”和“1”,则不能删除任何字符。
例子:
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”.
方法:以下是最大可能删除的情况
- Case-1:当所有的0或1都可以被删除时。当“0”和“ 1”的总数不同时,选择整个字符串并删除所有出现的少数元素。
- Case-2:当两个字符的数字相同时。这里选择整个字符串将无法删除任何字符。因此,以这样一种方式获取一个子字符串,即其中一个字符的计数与其在实际字符串中的计数相同,而另一个则少一个。那么可能的删除是(count of any 字符 in entire 字符串 – 1) 。
- Case-3:当字符串只包含一种字符时。则无法移除。
下面是上述方法的实现。
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 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 mp =
new Dictionary();
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
输出
3
时间复杂度: O(N)
辅助空间: O(1)