📌  相关文章
📜  要求删除的最小字符数,以便每个字符出现相同的次数

📅  最后修改于: 2021-04-17 15:29:35             🧑  作者: Mango

给定长度为N的字符串S ,任务是找到需要删除的最小字符数,以使字符串中的每个不同字符出现相同的次数。

例子:

方法:想法是使用多集和映射。请按照以下步骤解决问题:

  • 初始化一个map 表示countMap和一个multiset 表示countMultiset来存储每个字符的频率。
  • 将变量ans初始化为INT_MAX,以存储要删除的最小字符数。
  • countMap中遍历字符串SS [i]的递增计数
  • 遍历地图countMap并将字符的频率插入countMultiset中。
  • 找到多集countMultiset的大小,并将其存储在变量m中。
  • 遍历多集countMultiset并将ans更新为ans = min(ans,(N-(mi)*(* it)))),然后将i递增1
  • 最后,完成上述步骤后,将答案打印为ans。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum number of
// character removals required to make
// frequency of all distinct characters the same
int minimumDeletion(string s, int n)
{
    // Stores the frequency
    // of each character
    map countMap;
 
    // Traverse the string
    for (int i = 0; i < n; i++) {
        countMap[s[i]]++;
    }
 
    // Stores the frequency of each
    // charachter in sorted order
    multiset countMultiset;
 
    // Traverse the Map
    for (auto it : countMap) {
 
        // Insert the frequency in multiset
        countMultiset.insert(it.second);
    }
 
    // Stores the count of elements
    // required to be removed
    int ans = INT_MAX;
 
    int i = 0;
 
    // Stores the size of multiset
    int m = countMultiset.size();
 
    // Traverse the multiset
    for (auto j : countMultiset) {
        // Update the ans
        ans = min(ans, n - (m - i) * j);
 
        // Increment i by 1
        i++;
    }
 
    // Return
    return ans;
}
 
// Driver Code
int main()
{
    // Input
    string S = "geeksforgeeks";
    int N = S.length();
 
    cout << minimumDeletion(S, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find minimum number of
// character removals required to make
// frequency of all distinct characters the same
static int minimumDeletion(String s, int n)
{
     
    // Stores the frequency
    // of each character
    HashMap countMap = new HashMap<>();
 
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
        char ch = s.charAt(i);
        countMap.put(ch,
                     countMap.getOrDefault(ch, 0) + 1);
    }
 
    // Stores the frequency of each
    // charachter in sorted order
    ArrayList countMultiset = new ArrayList<>(
        countMap.values());
    Collections.sort(countMultiset);
 
    // Stores the count of elements
    // required to be removed
    int ans = Integer.MAX_VALUE;
 
    int i = 0;
 
    // Stores the size of multiset
    int m = countMultiset.size();
 
    // Traverse the multiset
    for(int j : countMultiset)
    {
         
        // Update the ans
        ans = Math.min(ans, n - (m - i) * j);
 
        // Increment i by 1
        i++;
    }
 
    // Return
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    // Input
    String S = "geeksforgeeks";
    int N = S.length();
 
    System.out.println(minimumDeletion(S, N));
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
import sys
 
# Function to find minimum number of
# character removals required to make
# frequency of all distinct characters the same
def minimumDeletion(s, n):
     
    # Stores the frequency
    # of each character
    countMap = {}
 
    # Traverse the string
    for i in s:
        countMap[i] = countMap.get(i, 0) + 1
 
    # Stores the frequency of each
    # charachter in sorted order
    countMultiset = []
 
    # Traverse the Map
    for it in countMap:
         
        # Insert the frequency in multiset
        countMultiset.append(countMap[it])
 
    # Stores the count of elements
    # required to be removed
    ans = sys.maxsize + 1
 
    i = 0
 
    # Stores the size of multiset
    m = len(countMultiset)
 
    # Traverse the multiset
    for j in sorted(countMultiset):
 
        # Update the ans
        ans = min(ans, n - (m - i) * j)
 
        # Increment i by 1
        i += 1
 
    # Return
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    S = "geeksforgeeks"
    N = len(S)
 
    print (minimumDeletion(S, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System.Collections.Generic;
using System;
 
class GFG{
 
// Function to find minimum number of
// character removals required to make
// frequency of all distinct characters the same
static int minimumDeletion(string s, int n)
{
     
    // Stores the frequency
    // of each character
    Dictionary countMap = new Dictionary();
                                               
    // Traverse the string
    for(int i = 0; i < n; i++)
    {
        if (countMap.ContainsKey(s[i]) == true)
        {
            countMap[s[i]] += 1;
        }
        else
        {
            countMap[s[i]] = 1;
        }
    }
 
    // Stores the frequency of each
    // charachter in sorted order
    List countMultiset = new List();
    foreach(var values in countMap.Values)
    {
        countMultiset.Add(values);
    }
    countMultiset.Sort();
     
    // Stores the count of elements
    // required to be removed
    int ans = 100000000;
    int index = 0;
 
    // Stores the size of multiset
    int m = countMultiset.Count;
 
    // Traverse the multiset
    foreach(var j in countMultiset)
    {
         
        // Update the ans
        ans = Math.Min(ans, n - (m - index) * j);
         
        // Increment i by 1
        index++;
    }
     
    // Return
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Input
    string S = "geeksforgeeks";
    int N = S.Length;
 
    Console.WriteLine(minimumDeletion(S, N));
}
}
 
// This code is contributed by Stream_Cipher


输出:
5

时间复杂度: O(N * log(N))
辅助空间: O(1)