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

📅  最后修改于: 2021-10-27 07:49:40             🧑  作者: Mango

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

例子:

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

  • 初始化一个 mapcountMap和一个 multisetcountMultiset来存储每个字符的频率。
  • 初始化一个变量 say ans为 INT_MAX 以存储要删除的最小字符数。
  • 遍历字符串S并在countMap 中递增S[i]计数。
  • 遍历地图countMap并在countMultiset 中插入字符的频率
  • 找到 multiset 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


Javascript


输出:
5

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程