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