Remove characters from string that appears strictly less than K times

Given a string of lowercase letters and a number K. The task is to reduce it by removing the characters which appears strictly less than K times in the string.

Examples:

Input : str = "geeksforgeeks", K = 2
Output : geeksgeeks

Input : str = "geeksforgeeks", K = 3
Output : eeee

Approach :

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to reduce the string by
// removing the characters which
// appears less than k times
  
#include <bits/stdc++.h>
using namespace std;
  
const int MAX_CHAR = 26;
  
// Function to reduce the string by
// removing the characters which
// appears less than k times
string removeChars(string str, int k)
{
    // Hash table initialised to 0
    int hash[MAX_CHAR] = { 0 };
  
    // Increment the frequency of the character
    int n = str.length();
    for (int i = 0; i < n; ++i)
        hash[str[i] - 'a']++;
  
    // create a new empty string
    string res = "";
    for (int i = 0; i < n; ++i) {
  
        // Append the characters which
        // appears more than equal to k times
        if (hash[str[i] - 'a'] >= k) {
            res += str[i];
        }
    }
  
    return res;
}
  
// Driver Code
int main()
{
    string str = "geeksforgeeks";
    int k = 2;
  
    cout << removeChars(str, k);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to reduce the string by
// removing the characters which
// appears less than k times
class GFG {
  
    final static int MAX_CHAR = 26;
  
// Function to reduce the string by
// removing the characters which
// appears less than k times
    static String removeChars(String str, int k) {
        // Hash table initialised to 0
        int hash[] = new int[MAX_CHAR];
  
        // Increment the frequency of the character
        int n = str.length();
        for (int i = 0; i < n; ++i) {
            hash[str.charAt(i) - 'a']++;
        }
  
        // create a new empty string
        String res = "";
        for (int i = 0; i < n; ++i) {
  
            // Append the characters which
            // appears more than equal to k times
            if (hash[str.charAt(i) - 'a'] >= k) {
                res += str.charAt(i);
            }
        }
  
        return res;
    }
  
// Driver Code
    static public void main(String[] args) {
        String str = "geeksforgeeks";
        int k = 2;
  
        System.out.println(removeChars(str, k));
    }
}
  
// This code is contributed by 29AjayKumar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to reduce the string 
# by removing the characters which
# appears less than k times
MAX_CHAR = 26
  
# Function to reduce the string by
# removing the characters which
# appears less than k times
def removeChars(str, k):
  
    # Hash table initialised to 0
    hash = [0] * (MAX_CHAR)
  
    # Increment the frequency of 
    # the character
    n = len(str)
    for i in range(n):
        hash[ord(str[i]) - ord('a')] += 1
  
    # create a new empty string
    res = ""
    for i in range(n):
  
        # Append the characters which
        # appears more than equal to k times
        if (hash[ord(str[i]) - ord('a')] >= k) :
            res += str[i]
  
    return res
  
# Driver Code
if __name__ == "__main__":
      
    str = "geeksforgeeks"
    k = 2
  
    print(removeChars(str, k))
  
# This code is contributed by ita_c
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to reduce the string by
// removing the characters which
// appears less than k times
using System;
class GFG 
{
  
    readonly static int MAX_CHAR = 26;
  
    // Function to reduce the string by
    // removing the characters which
    // appears less than k times
    static String removeChars(String str, int k)  
    {
        // Hash table initialised to 0
        int []hash = new int[MAX_CHAR];
  
        // Increment the frequency of the character
        int n = str.Length;
        for (int i = 0; i < n; ++i) 
        {
            hash[str[i] - 'a']++;
        }
  
        // create a new empty string
        String res = "";
        for (int i = 0; i < n; ++i) 
        {
  
            // Append the characters which
            // appears more than equal to k times
            if (hash[str[i] - 'a'] >= k) 
            {
                res += str[i];
            }
        }
  
        return res;
    }
  
    // Driver Code
    static public void Main() 
    {
        String str = "geeksforgeeks";
        int k = 2;
  
        Console.WriteLine(removeChars(str, k));
    }
}
  
// This code is contributed by Rajput-Ji
chevron_right

Output:
geeksgeeks

Time Complexity: O(N), where N is the length of the given string.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Article Tags :
Practice Tags :