📜  除去ķ字符后,最大非重复字符

📅  最后修改于: 2021-10-27 16:53:56             🧑  作者: Mango

给定一个字符串S ,其中包含长度为N 的小写英文字母和一个整数K ,使得K ≤ N 。任务是从字符串删除K 个字符后找到最大数量的非重复字符。

例子:

朴素的方法:朴素的想法是删除给定字符串所有可能的K 个字符,然后在所有形成的字符串找到不重复的字符。打印所有非重复字符数中的最大值。
时间复杂度: O(N!),其中 N 是给定字符串的长度。
辅助空间: O(NK)

高效的方法:为了优化上述方法,

以下是步骤:

  1. 创建一个哈希表来存储每个元素的频率。
  2. 将每个元素的频率插入向量V 中,并按升序对向量V进行排序。
  3. 对于向量V 的每个元素(比如currentElement ),找到KcurrentElement – 1 中的最小值,并将KV[i]减少两者中的最小值。
  4. 重复上述步骤,直到K不为零。
  5. 向量V1的计数给出了删除K 个字符后的最大非重复字符数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find maximum distinct
// character after removing K character
int maxDistinctChar(string s, int n, int k)
{
    // Freq implemented as hash table to
    // store frequency of each character
    unordered_map freq;
 
    // Store frequency of each character
    for (int i = 0; i < n; i++)
        freq[s[i]]++;
 
    vector v;
 
    // Insert each frequency in v
    for (auto it = freq.begin();
         it != freq.end(); it++) {
        v.push_back(it->second);
    }
 
    // Sort the freq of character in
    // non-decresing order
    sort(v.begin(), v.end());
 
    // Traverse the vector
    for (int i = 0; i < v.size(); i++) {
        int mn = min(v[i] - 1, k);
 
        // Update v[i] and k
        v[i] -= mn;
        k -= mn;
    }
 
    // If K is still not 0
    if (k > 0) {
 
        for (int i = 0; i < v.size(); i++) {
            int mn = min(v[i], k);
            v[i] -= mn;
            k -= mn;
        }
    }
 
    // Store the final answer
    int res = 0;
    for (int i = 0; i < v.size(); i++)
 
        // Count this character if freq 1
        if (v[i] == 1)
            res++;
 
    // Return count of distinct characters
    return res;
}
 
// Driver Code
int main()
{
    // Given string
    string S = "geeksforgeeks";
 
    int N = S.size();
 
    // Given k
    int K = 1;
 
    // Function Call
    cout << maxDistinctChar(S, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find maximum distinct
// character after removing K character
static int maxDistinctChar(char []s, int n, int k)
{
    // Freq implemented as hash table to
    // store frequency of each character
    HashMap freq  = new HashMap();
 
    // Store frequency of each character
    for (int i = 0; i < n; i++)
    {
        if(freq.containsKey((int)s[i]))
        {
            freq.put((int)s[i],
                     freq.get((int)s[i]) + 1);
        }
        else
        {
            freq.put((int)s[i], 1);
        }
    }
 
    Vector v = new Vector();
 
    // Insert each frequency in v
    for (Map.Entry it : freq.entrySet())
    {
        v.add(it.getValue());
    }
 
    // Sort the freq of character in
    // non-decresing order
    Collections.sort(v);
 
    // Traverse the vector
    for (int i = 0; i < v.size(); i++)
    {
        int mn = Math.min(v.get(i) - 1, k);
 
        // Update v[i] and k
        v.set(i, v.get(i) - mn);
        k -= mn;
    }
 
    // If K is still not 0
    if (k > 0)
    {
        for (int i = 0; i < v.size(); i++)
        {
            int mn = Math.min(v.get(i), k);
            v.set(i, v.get(i) - mn);
            k -= mn;
        }
    }
 
    // Store the final answer
    int res = 0;
    for (int i = 0; i < v.size(); i++)
 
        // Count this character if freq 1
        if (v.get(i) == 1)
            res++;
 
    // Return count of distinct characters
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given String
    String S = "geeksforgeeks";
 
    int N = S.length();
 
    // Given k
    int K = 1;
 
    // Function Call
    System.out.print(maxDistinctChar(S.toCharArray(),
                                     N, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
from collections import defaultdict
 
# Function to find maximum distinct
# character after removing K character
def maxDistinctChar(s, n, k):
 
    # Freq implemented as hash table to
    # store frequency of each character
    freq = defaultdict (int)
 
    # Store frequency of each character
    for i in range (n):
        freq[s[i]] += 1
 
    v = []
 
    # Insert each frequency in v
    for it in freq.values():
        v.append(it)
 
    # Sort the freq of character in
    # non-decresing order
    v.sort()
 
    # Traverse the vector
    for i in range (len(v)):
        mn = min(v[i] - 1, k)
 
        # Update v[i] and k
        v[i] -= mn
        k -= mn
 
    # If K is still not 0
    if (k > 0):
        for i in range (len(v)):
            mn = min(v[i], k);
            v[i] -= mn
            k -= mn
 
    # Store the final answer
    res = 0
    for i in range (len(v)):
 
        # Count this character if freq 1
        if (v[i] == 1):
            res += 1
 
    # Return count of distinct characters
    return res
 
# Driver Code
if __name__ == "__main__":
   
    # Given string
    S = "geeksforgeeks"
 
    N = len(S)
 
    # Given k
    K = 1
 
    # Function Call
    print(maxDistinctChar(S, N, K))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to find maximum distinct
// character after removing K character
static int maxDistinctChar(char []s, int n, int k)
{
     
    // Freq implemented as hash table to
    // store frequency of each character
    Dictionary freq = new Dictionary();
 
    // Store frequency of each character
    for(int i = 0; i < n; i++)
    {
        if(freq.ContainsKey((int)s[i]))
        {
            freq[(int)s[i]] = freq[(int)s[i]] + 1;
        }
        else
        {
            freq.Add((int)s[i], 1);
        }
    }
 
    List v = new List();
 
    // Insert each frequency in v
    foreach(KeyValuePair it in freq)
    {
        v.Add(it.Value);
    }
 
    // Sort the freq of character in
    // non-decresing order
    v.Sort();
 
    // Traverse the vector
    for(int i = 0; i < v.Count; i++)
    {
        int mn = Math.Min(v[i] - 1, k);
 
        // Update v[i] and k
        v[i] = v[i] - mn;
        k -= mn;
    }
 
    // If K is still not 0
    if (k > 0)
    {
        for(int i = 0; i < v.Count; i++)
        {
            int mn = Math.Min(v[i], k);
            v[i] = v[i] - mn;
            k -= mn;
        }
    }
 
    // Store the readonly answer
    int res = 0;
    for(int i = 0; i < v.Count; i++)
 
        // Count this character if freq 1
        if (v[i] == 1)
            res++;
 
    // Return count of distinct characters
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String
    String S = "geeksforgeeks";
 
    int N = S.Length;
 
    // Given k
    int K = 1;
 
    // Function call
    Console.Write(maxDistinctChar(S.ToCharArray(),
                                  N, K));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
4

时间复杂度: O(N)
辅助空间: O(26)

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