Given a string of lowercase letters str of length N, the task is to reduce it by removing the characters which appear exactly K times in the string.
Examples:
Input: str = “geeksforgeeks”, K = 2
Output: eeforeeInput: str = “geeksforgeeks”, K = 4
Output: gksforgks
Approach:
- Create a hash table of size 26, where 0th index represents ‘a’ and 1st index represent ‘b’ and so on.
- Initialize the hash table to zero.
- Iterate through the string and increment the frequency of each character(s[i]) in the hash table
- Now, once again traverse through the string and append the characters, with frequency K, in the new string.
Below is the implementation of the above approach:
C++
// C++ program to remove characters from // a String that appears exactly 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 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 |
eeforee
Time Complexity: O(N)
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.
Recommended Posts:
- Remove characters from string that appears strictly less than K times
- Check if max occurring character of one string appears same no. of times in other
- Longest subsequence where every character appears at-least k times
- Find the only element that appears b times
- Count all elements in the array which appears at least K times after their first occurrence
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Count number of times each Edge appears in all possible paths of a given Tree
- Smallest String consisting of a String S exactly K times as a Substring
- Minimum cost to remove the spaces between characters of a String by rearranging the characters
- Maximized partitions of a string such that each character of the string appears in one substring
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Remove characters that appear more than k times
- String which when repeated exactly K times gives a permutation of S
- Count of times second string can be formed from the characters of first string
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Find if string is K-Palindrome or not using all characters exactly once
- Find the String having each substring with exactly K distinct characters
- Remove characters from the first string which are present in the second string
- Count words that appear exactly two times in an array of words
- Smallest element in an array that is repeated exactly 'k' times.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

