📌  相关文章
📜  删除出现超过 k 次的字符

📅  最后修改于: 2021-10-27 08:47:17             🧑  作者: Mango

给定字符串小写字母,通过删除在字符串出现超过 k 次的字符来减少它。
例子:

Input : str = "geeksforgeeks"
        k = 2
Output : for

Input : str = "geeksforgeeks"
        k = 3
Output : gksforgks

方法 :

  • 创建一个包含 26 个索引的哈希表,其中第 0 个索引代表“a”,第 1 个索引代表“b”,依此类推。将哈希表初始化为零。
  • 遍历字符串并计数递增哈希表中 str[i]字符的频率。
  • 现在再次遍历字符串并将那些在哈希表中出现的频率小于 k 的字符附加到新字符串,并跳过那些出现大于等于 k 的字符。

时间复杂度– O(N)
下面是上述方法的实现:

C++
// C++ program to reduce the string by
// removing the characters which
// appears more than k times
#include 
using namespace std;
 
const int MAX_CHAR = 26;
 
void removeChars(char arr[], int k)
{
    // Hash table initialised to 0
    int hash[MAX_CHAR] = { 0 };
 
    // Increment the frequency of the character
    int n = strlen(arr);
    for (int i = 0; i < n; ++i)
        hash[arr[i] - 'a']++;   
 
    // Next index in reduced string
    int index = 0;
    for (int i = 0; i < n; ++i) {
 
        // Append the characters which
        // appears less than k times
        if (hash[arr[i] - 'a'] < k) {
           arr[index++] = arr[i];
        }
    }
 
    arr[index] = '\0';
}
 
int main()
{
    char str[] = "geeksforgeeks";
    int k = 2;
    removeChars(str, k);
    cout << str;
    return 0;
}


Java
// Java program to reduce the string by
// removing the characters which
// appears more than k times
import java.util.*;
class Solution
{
  
static final int MAX_CHAR = 26;
  
static void removeChars(char arr[], int k)
{
    // Hash table initialised to 0
    int hash[]=new int[MAX_CHAR];
     
    for (int i = 0; i 


Python3
# Python 3 program to reduce the string by
# removing the characters which
# appears more than k times
MAX_CHAR = 26
 
def removeChars(arr, k):
     
    # Hash table initialised to 0
    hash = [0 for i in range(MAX_CHAR)]
 
    # Increment the frequency of the character
    n = len(arr)
    for i in range(n):
        hash[ord(arr[i]) - ord('a')] += 1
 
    # Next index in reduced string
    index = 0
    for i in range(n):
         
        # Append the characters which
        # appears less than k times
        if (hash[ord(arr[i]) - ord('a')] < k):
            arr[index] = arr[i]
            index += 1
         
    arr[index] = ' '
     
    for i in range(index):
        print(arr[i], end = '')
 
# Driver code
if __name__ == '__main__':
    str = "geeksforgeeks"
    str = list(str)
    k = 2
    removeChars(str, k)
 
# This code is contributed by
# Shashank_Sharma


C#
// C# program to reduce the string by
// removing the characters which
// appears more than k times
using System;
public class Solution{
 
    static readonly int MAX_CHAR = 26;
 
    static void removeChars(char []arr, int k)
    {
        // Hash table initialised to 0
        int []hash=new int[MAX_CHAR];
 
        for (int i = 0; i 


PHP


Javascript


Python3
# Python 3 program to reduce the string
# by removing the characters which
# appears more than k times
from collections import Counter
 
# Function to reduce the string by
# removing the characters which
# appears more than k times
def removeChars(str, k):
   
    # Using Counter function to
    # count frequencies
    freq = Counter(str)
     
    # create a new empty string
    res = ""
 
    for i in range(len(str)):
 
        # Append the characters which
        # appears less than equal to k times
        if (freq[str[i]] < k):
            res += str[i]
 
    return res
 
 
# Driver Code
if __name__ == "__main__":
 
    str = "geeksforgeeks"
    k = 2
 
    print(removeChars(str, k))
 
# This code is contributed by vikkycirus


输出:
for

方法 #2:使用内置Python函数:

  • 我们将使用内置Counter()函数扫描字符串并计算所有字符的出现次数。
  • 现在再次遍历字符串并将那些在频率字典中出现的频率小于 k 的字符附加到新字符串,并跳过那些出现大于等于 k 的字符。

注意:此方法适用于所有类型的字符。

下面是上述方法的实现:

蟒蛇3

# Python 3 program to reduce the string
# by removing the characters which
# appears more than k times
from collections import Counter
 
# Function to reduce the string by
# removing the characters which
# appears more than k times
def removeChars(str, k):
   
    # Using Counter function to
    # count frequencies
    freq = Counter(str)
     
    # create a new empty string
    res = ""
 
    for i in range(len(str)):
 
        # Append the characters which
        # appears less than equal to k times
        if (freq[str[i]] < k):
            res += str[i]
 
    return res
 
 
# Driver Code
if __name__ == "__main__":
 
    str = "geeksforgeeks"
    k = 2
 
    print(removeChars(str, k))
 
# This code is contributed by vikkycirus

输出:

for

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