text: Check if K palindromic strings can be formed from a given string | C ++ 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 ; iterator for map ; storing the frequency of every character in map ; if K is greater than size of string 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 other wise true ; Driver code
code: #include <iostream> NEW_LINE #include <map> NEW_LINE using namespace std ; bool can_Construct ( string S , int K ) { map < int , int > m ; int i = 0 , j = 0 , p = 0 ; if ( S . length ( ) == K ) { return true ; } map < int , int > :: iterator h ; for ( i = 0 ; i < S . length ( ) ; i ++ ) { m [ S [ i ] ] = m [ S [ i ] ] + 1 ; } if ( K > S . length ( ) ) { return false ; } else { for ( h = m . begin ( ) ; h != m . end ( ) ; h ++ ) { if ( m [ h -> first ] % 2 != 0 ) { p = p + 1 ; } } } if ( K < p ) { return false ; } return true ; } int main ( ) { string S = " annabelle " ; int K = 4 ; if ( can_Construct ( S , K ) ) { cout << " Yes " ; } else { cout << " No " ; } }