Find if string is K | Java program to find if String is K - Palindrome or not using all characters exactly once ; When size of String is less than k ; When size of String is equal to k ; When size of String is greater than k to store the frequencies of the characters ; To store the count of characters whose number of occurrences is odd . ; Iterating over the map ; Driver code
import java . util . * ; class GFG { static void iskPalindromesPossible ( String s , int k ) { if ( s . length ( ) < k ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); return ; } if ( s . length ( ) == k ) { System . out . print ( " Possible " + "NEW_LINE"); return ; } HashMap < Character , Integer > freq = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( freq . containsKey ( s . charAt ( i ) ) ) { freq . put ( s . charAt ( i ) , freq . get ( s . charAt ( i ) ) + 1 ) ; } else { freq . put ( s . charAt ( i ) , 1 ) ; } int count = 0 ; for ( Map . Entry < Character , Integer > it : freq . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) count ++ ; } if ( count > k ) System . out . print ( " No " + "NEW_LINE"); else System . out . print ( " Yes " + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " poor " ; int K = 3 ; iskPalindromesPossible ( str , K ) ; str = " geeksforgeeks " ; K = 10 ; iskPalindromesPossible ( str , K ) ; } }