Find if string is K | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void iskPalindromesPossible ( string s , int k ) { if ( s . size ( ) < k ) { cout << " Not ▁ Possible " << endl ; return ; } if ( s . size ( ) == k ) { cout << " Possible " << endl ; return ; } map < char , int > freq ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) freq [ s [ i ] ] ++ ; int count = 0 ; for ( auto it : freq ) { if ( it . second % 2 == 1 ) count ++ ; } if ( count > k ) cout << " No " << endl ; else cout << " Yes " << endl ; } int main ( ) { string str = " poor " ; int K = 3 ; iskPalindromesPossible ( str , K ) ; str = " geeksforgeeks " ; K = 10 ; iskPalindromesPossible ( str , K ) ; return 0 ; }