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