📜  从刚好出现 K 次的字符串中删除字符

📅  最后修改于: 2021-09-07 02:34:35             🧑  作者: Mango

给定字符串长度为N的小写字母str ,任务是通过删除字符串恰好出现K次的字符来减少它。
例子:

方法:

  • 创建一个大小为26的哈希表,其中第 0 个索引表示‘a’第 1 个索引表示‘b’ ,依此类推。
  • 将哈希表初始化为零。
  • 遍历字符串并增加哈希表中每个字符( s[i] )的频率
  • 现在,再次遍历字符串并将频率为 K 的字符附加到新字符串。

下面是上述方法的实现:

C++
// C++ program to remove characters from
// a String that appears exactly K times
 
#include 
using namespace std;
 
const int MAX_CHAR = 26;
 
// Function to reduce the string by
// removing the characters which
// appears exactly k times
string 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']++;
 
    // To store answer
    string ans = "";
 
    // Next index in reduced string
    int index = 0;
    for (int i = 0; i < n; ++i) {
 
        // Append the characters which
        // appears exactly k times
        if (hash[arr[i] - 'a'] != k) {
            ans += arr[i];
        }
    }
 
    return ans;
}
 
// Driver code
int main()
{
    char str[] = "geeksforgeeks";
    int k = 2;
 
    // Function call
    cout << removeChars(str, k);
 
    return 0;
}


Java
// Java program to remove characters from
// a String that appears exactly K times
import java.util.*;
 
class GFG{
  
static int MAX_CHAR = 26;
  
// Function to reduce the String by
// removing the characters which
// appears exactly k times
static String removeChars(char arr[], int k)
{
    // Hash table initialised to 0
    int []hash = new int[MAX_CHAR];
  
    // Increment the frequency
    // of the character
    int n = arr.length;
    for (int i = 0; i < n; ++i)
        hash[arr[i] - 'a']++;
  
    // To store answer
    String ans = "";
 
    for (int i = 0; i < n; ++i) {
  
        // Append the characters which
        // appears exactly k times
        if (hash[arr[i] - 'a'] != k) {
            ans += arr[i];
        }
    }
  
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    char str[] = "geeksforgeeks".toCharArray();
    int k = 2;
  
    // Function call
    System.out.print(removeChars(str, k));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to remove characters from
# a String that appears exactly K times
 
MAX_CHAR = 26
 
# Function to reduce the string by
# removing the characters which
# appears exactly k times
def removeChars(arr, k):
 
    # Hash table initialised to 0
    hash = [0]*MAX_CHAR
 
    # Increment the frequency
    # of the character
    n = len(arr)
    for i in range( n):
        hash[ord(arr[i]) - ord('a')] += 1
 
    # To store answer
    ans = ""
 
    # Next index in reduced string
    index = 0
    for i in range(n):
 
        # Append the characters which
        # appears exactly k times
        if (hash[ord(arr[i]) - ord('a')] != k):
            ans += arr[i]
         
    return ans
 
# Driver code
if __name__ =="__main__":
    str = "geeksforgeeks"
    k = 2
 
    # Function call
    print(removeChars(str, k))
 
# This code is contributed by chitranayal


C#
// C# program to remove characters from
// a String that appears exactly K times
using System;
 
class GFG{
   
static int MAX_CHAR = 26;
   
// Function to reduce the String by
// removing the characters which
// appears exactly k times
static String removeChars(char []arr, int k)
{
    // Hash table initialised to 0
    int []hash = new int[MAX_CHAR];
   
    // Increment the frequency
    // of the character
    int n = arr.Length;
    for (int i = 0; i < n; ++i)
        hash[arr[i] - 'a']++;
   
    // To store answer
    String ans = "";
  
    for (int i = 0; i < n; ++i) {
   
        // Append the characters which
        // appears exactly k times
        if (hash[arr[i] - 'a'] != k) {
            ans += arr[i];
        }
    }
   
    return ans;
}
   
// Driver code
public static void Main(String[] args)
{
    char []str = "geeksforgeeks".ToCharArray();
    int k = 2;
   
    // Function call
    Console.Write(removeChars(str, k));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
eeforee

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live