📜  使字符串空所需的操作

📅  最后修改于: 2021-06-26 17:15:10             🧑  作者: Mango

给定一个字符串str ,任务是通过给定的操作使该字符串空。在单个操作中,您可以选择字符串的某些字符(每个被选择的字符应具有相同的频率)并将其从字符串删除。打印使字符串空所需的全部操作。

例子:

方法:查找字符串字符的唯一频率。唯一频率的总数将是使字符串空所需的操作数。
对于str =“ aaabbbcccc” ,唯一频率为34 。唯一频率的总数为2
HashMap可用于存储字符及其频率,然后HashSet可用于查找唯一频率的计数,这是所需的操作次数。

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include 
using namespace std;
  
// Function to return the count of operations required
int totalOperations(string str, int len)
{
    // HashMap to store characters and their frequencies
    unordered_map h;
    for (int i = 0; i < len; i++)
  
        // If already contains the character then
        // increment its frequency by 1
        h[str[i]]++;
  
    // HashSet to store unique frequency
    unordered_set hs;
  
    // Insert frequencies into HashSet
    for (auto i : h)
        hs.insert(i.second);
  
    // Count of unique frequencies
    return hs.size();
}
  
// Driver Code
int main()
{
    string str = "geeksforgeeks";
    int len = str.length();
  
    cout << totalOperations(str, len) << endl;
    return 0;
}
  
// This code is contributed by
// sanjeev2552


Java
// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Function to return the count of operations required
    static int totalOperations(String str, int len)
    {
  
        // HashMap to store characters and their frequencies
        HashMap h = new HashMap();
        for (int i = 0; i < len; i++) {
  
            // If already contains the character then
            // increment its frequency by 1
            if (h.containsKey(str.charAt(i)))
                h.put(str.charAt(i), h.get(str.charAt(i)) + 1);
  
            // Else add the character to the HashMap with frequency 1
            else
                h.put(str.charAt(i), 1);
        }
  
        // Set to iterate over HashMap
        Set > set = h.entrySet();
  
        // HashSet to store unique frequency
        HashSet hs = new HashSet();
  
        // Insert frequencies into HashSet
        for (Map.Entry me : set)
            hs.add(me.getValue());
  
        // Count of unique frequencies
        return hs.size();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int len = str.length();
        System.out.println(totalOperations(str, len));
    }
}


Python3
# Python implementation of the approach 
  
# Function to return the count of operations required
def totalOperations(st, length):
      
    # Dictionary to store characters and their frequencies
    d = {}
    for i in range(length):
          
        # If already contains the character then 
        # increment its frequency by 1 
        if st[i] in d:
            d[st[i]] += 1
          
        # Else add the character to the HashMap with frequency 1
        else:
            d[st[i]] = 1
  
    # Set to Store unique frequency 
    valueSet = set()
  
    # Insert frequencies into HashSet 
    for key in d.keys():
        valueSet.add(d[key])
      
    # Count of unique frequencies
    return len(valueSet)
  
# Driver Code
st = "geeksforgeeks"
l = len(st)
print(totalOperations(st, l))
  
# This code is contributed by Vivekkumar Singh


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{ 
  
// Function to return 
// the count of operations required 
static int totalOperations(String str, int len) 
{ 
  
    // HashMap to store characters
    // and their frequencies 
    Dictionary h = new Dictionary();
    for (int i = 0; i < len; i++) 
    { 
  
        // If already contains the character then 
        // increment its frequency by 1 
        if (h.ContainsKey(str[i])) 
            h[str[i]] = h[str[i]] + 1; 
  
        // Else add the character 
        // to the HashMap with frequency 1 
        else
            h.Add(str[i], 1); 
    } 
  
    // Set to iterate over HashMap 
    // HashSet to store unique frequency 
    HashSet hs = new HashSet(); 
  
    // Insert frequencies into HashSet 
    foreach(KeyValuePair me in h)
        hs.Add(me.Value); 
  
    // Count of unique frequencies 
    return hs.Count; 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    String str = "geeksforgeeks"; 
    int len = str.Length; 
    Console.WriteLine(totalOperations(str, len)); 
} 
} 
  
// This code is contributed by Rajput-Ji


输出:
3

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。