Check if K palindromic strings can be formed from a given string | Java program to check whether the string is K palindrome or not ; Function to check whether the string is K palindrome or not ; Map to frequency of character ; Check when k is given as same as length of string ; Storing the frequency of every character in map ; If K is greater than size of then return false ; Check that number of character having the odd frequency ; If k is less than number of odd frequency character then it is again false otherwise true ; Driver Code
import java . util . * ; class GFG { static boolean can_Construct ( String S , int K ) { Map < Character , Integer > m = new HashMap < > ( ) ; int p = 0 ; if ( S . length ( ) == K ) return true ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) m . put ( S . charAt ( i ) , m . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; if ( K > S . length ( ) ) return false ; else { for ( Integer h : m . values ( ) ) { if ( h % 2 != 0 ) p = p + 1 ; } } if ( K < p ) return false ; return true ; } public static void main ( String [ ] args ) { String S = " annabelle " ; int K = 4 ; if ( can_Construct ( S , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }