📌  相关文章
📜  最大化可以从给定的二进制字符串子字符串中完成的少数字符删除

📅  最后修改于: 2022-05-13 01:56:09.569000             🧑  作者: Mango

最大化可以从给定的二进制字符串子字符串中完成的少数字符删除

Python

给定大小为N的二进制字符串str 。从字符串中选择任意一个子字符串,并从子字符串中删除所有出现的少数字符(即频率较低的字符)。任务是找出可以从执行一项此类操作中删除的最大字符数。

注意:如果任何子字符串在相同的数字中同时包含“0”和“1”,则不能删除任何字符。

例子:

方法:以下是最大可能删除的情况

  • Case-1:当所有的01都可以被删除时。当“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)