Given a string **S** of size **N** and an integer **K**, the task is to find whether the characters of the string can be arranged to make **K** palindromic strings simultaneously.

**Examples:**

Input:S = “annabelle”, K = 2

Output:Yes

Explanation:

All characters of string S can be distributed into “elble” and “anna” which are both palindromic.

Input:S =”abcd”, K = 4

Output:Yes

Explanation:

Partition all characters of string as single character.

**Approach**

- If the frequency of every character is even and K lies between 1 and N then it is always possible to form K palindrome strings.
- But if there are some characters (say
**odd_count**) with odd frequency, then K must lie between odd_count and N for K palindromic strings to be possible.

Hence, follow the steps below to solve the problem:

- If K exceeds the length of the string, straightaway print
- Store the frequency of all character in a Map.
- Count the number of characters having odd frequency.
- If the count is less then given K, then print “No”. Otherwise print “Yes”.

**“No”**.

Below is the implementation of the above approach:

## C++

`// C++ program to check ` `// whether the string is ` `// K palindrome or not ` `#include <iostream> ` `#include <map> ` `using` `namespace` `std; ` ` ` `// function to check ` `// whether the string is ` `// K palindrome or not ` `bool` `can_Construct(string S, ` `int` `K) ` `{ ` ` ` `// map to frequency of character ` ` ` `map<` `int` `, ` `int` `> m; ` ` ` ` ` `int` `i = 0, j = 0, p = 0; ` ` ` ` ` `// Check when k is given ` ` ` `// as same as length of string ` ` ` `if` `(S.length() == K) { ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// iterator for map ` ` ` `map<` `int` `, ` `int` `>::iterator h; ` ` ` ` ` `// stroing the frequency of every ` ` ` `// character in map ` ` ` `for` `(i = 0; i < S.length(); i++) { ` ` ` `m[S[i]] = m[S[i]] + 1; ` ` ` `} ` ` ` ` ` `// if K is greater than size ` ` ` `// of string then return false ` ` ` `if` `(K > S.length()) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// check that number of character ` ` ` `// having the odd frequency ` ` ` `for` `(h = m.begin(); h != m.end(); h++) { ` ` ` ` ` `if` `(m[h->first] % 2 != 0) { ` ` ` `p = p + 1; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// if k is less than number of odd ` ` ` `// frequecny character then it is ` ` ` `// again false other wise true ` ` ` `if` `(K < p) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string S = ` `"annabelle"` `; ` ` ` `int` `K = 4; ` ` ` ` ` `if` `(can_Construct(S, K)) { ` ` ` `cout << ` `"Yes"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"No"` `; ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to check whether ` `# the is K palindrome or not ` ` ` `# Function to check whether ` `# the is K palindrome or not ` `def` `can_Construct(S, K): ` ` ` ` ` `# map to frequency of character ` ` ` `m ` `=` `dict` `() ` ` ` `p ` `=` `0` ` ` ` ` `# Check when k is given ` ` ` `# as same as length of string ` ` ` `if` `(` `len` `(S) ` `=` `=` `K): ` ` ` `return` `True` ` ` ` ` `# Stroing the frequency of every ` ` ` `# character in map ` ` ` `for` `i ` `in` `S: ` ` ` `m[i] ` `=` `m.get(i, ` `0` `) ` `+` `1` ` ` ` ` `# If K is greater than size ` ` ` `# of then return false ` ` ` `if` `(K > ` `len` `(S)): ` ` ` `return` `False` ` ` ` ` `else` `: ` ` ` ` ` `# Check that number of character ` ` ` `# having the odd frequency ` ` ` `for` `h ` `in` `m: ` ` ` `if` `(m[h] ` `%` `2` `!` `=` `0` `): ` ` ` `p ` `=` `p ` `+` `1` ` ` ` ` `# If k is less than number of odd ` ` ` `# frequecny character then it is ` ` ` `# again false otherwise true ` ` ` `if` `(K < p): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `S ` `=` `"annabelle"` ` ` `K ` `=` `4` ` ` ` ` `if` `(can_Construct(S, K)): ` ` ` `print` `(` `"Yes"` `) ` ` ` `else` `: ` ` ` `print` `(` `"No"` `) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

Yes

* Time Complexity: O (N)*.

*.*

**Auxillary Space:**O (N)Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Print all possible palindromic string formed using any pair of given strings
- Longest palindromic String formed using concatenation of given strings in any order
- Check if given string can be formed by two other strings or their permutations
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Check if all the palindromic sub-strings are of odd length
- Find all palindromic sub-strings of a given string | Set 2
- Find all distinct palindromic sub-strings of a given string
- Number of strings of length N with no palindromic sub string
- Count of unique palindromic strings of length X from given string
- Distinct palindromic sub-strings of the given string using Dynamic Programming
- Count pairs of non-overlapping palindromic sub-strings of the given string
- All possible strings of any length that can be formed from a given string
- Count of pairs of strings whose concatenation forms a palindromic string
- Count of strings that can be formed from another string using each character at-most once
- Find the number of strings formed using distinct characters of a given string
- Check if a string contains a palindromic sub-string of even length
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check if a string can be split into even length palindromic substrings

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.