Given a string **str** and an integer **K**, the task is to check if it is possible to make the string **K palindromes** using all the characters of the string exactly once.**Examples:**

Input:str = “poor”, K = 3Output:Yes

One way of getting 3 palindromes is: oo, p, r

Input:str = “fun”, K = 5Output:No

2 palindromes can’t be constructed using 3 distinct letters. Hence not possible.

**Approach:**

- If the size of the string is less than k, getting k palindromes is not possible.
- If the size of the string is equal to k, getting k palindromes is always possible. We give 1 character to each of k strings and all of them are palindromes as a string of length 1 is always a palindrome.
- If the size of the string is greater than k then some strings might have more than 1 character.
- Create a hashmap to store the frequency of every character as the characters having even number of occurrences can be distributed in groups of 2.
- Check if the number of characters having an odd number of occurrences is less than or equal to k, we can say that k palindromes can always be generated else no.

Below is the implementation of the above approach:

## C++

`// C++ program to find if string is K-Palindrome ` `// or not using all characters exactly once ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `void` `iskPalindromesPossible(string s, ` `int` `k) ` `{ ` ` ` `// when size of string is less than k ` ` ` `if` `(s.size() < k) { ` ` ` `cout << ` `"Not Possible"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// when size of string is equal to k ` ` ` `if` `(s.size() == k) { ` ` ` `cout << ` `"Possible"` `<< endl; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// when size of string is greater than k ` ` ` ` ` `// to store the frequencies of the characters ` ` ` `map<` `char` `, ` `int` `> freq; ` ` ` `for` `(` `int` `i = 0; i < s.size(); i++) ` ` ` `freq[s[i]]++; ` ` ` ` ` `// to store the count of characters ` ` ` `// whose number of occurrences is odd. ` ` ` `int` `count = 0; ` ` ` ` ` `// iterating over the map ` ` ` `for` `(` `auto` `it : freq) { ` ` ` `if` `(it.second % 2 == 1) ` ` ` `count++; ` ` ` `} ` ` ` `if` `(count > k) ` ` ` `cout << ` `"No"` `<< endl; ` ` ` `else` ` ` `cout << ` `"Yes"` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `string str = ` `"poor"` `; ` ` ` `int` `K = 3; ` ` ` `iskPalindromesPossible(str, K); ` ` ` ` ` `str = ` `"geeksforgeeks"` `; ` ` ` `K = 10; ` ` ` `iskPalindromesPossible(str, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Find if string is K-Palindrome or not using all characters exactly once ` `# Python 3 program to find if string is K-Palindrome ` `# or not using all characters exactly once ` ` ` `def` `iskPalindromesPossible(s, k): ` ` ` ` ` `# when size of string is less than k ` ` ` `if` `(` `len` `(s)<k): ` ` ` `print` `(` `"Not Possible"` `) ` ` ` `return` ` ` ` ` `# when size of string is equal to k ` ` ` `if` `(` `len` `(s) ` `=` `=` `k): ` ` ` `print` `(` `"Possible"` `) ` ` ` `return` ` ` ` ` `# when size of string is greater than k ` ` ` ` ` `# to store the frequencies of the characters ` ` ` `freq ` `=` `dict` `.fromkeys(s,` `0` `) ` ` ` `for` `i ` `in` `range` `(` `len` `(s)): ` ` ` `freq[s[i]] ` `+` `=` `1` ` ` ` ` `#to store the count of characters ` ` ` `# whose number of occurrences is odd. ` ` ` `count ` `=` `0` ` ` ` ` `# iterating over the map ` ` ` `for` `value ` `in` `freq.values(): ` ` ` `if` `(value ` `%` `2` `=` `=` `1` `): ` ` ` `count ` `+` `=` `1` ` ` `if` `(count > k): ` ` ` `print` `(` `"No"` `) ` ` ` `else` `: ` ` ` `print` `(` `"Yes"` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `str1 ` `=` `"poor"` ` ` `K ` `=` `3` ` ` `iskPalindromesPossible(str1, K) ` ` ` ` ` `str` `=` `"geeksforgeeks"` ` ` `K ` `=` `10` ` ` `iskPalindromesPossible(` `str` `, K) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find if String ` `// is K-Palindrome or not using ` `// all characters exactly once ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `static` `void` `iskPalindromesPossible(String s, ` ` ` `int` `k) ` `{ ` ` ` ` ` `// When size of String is less than k ` ` ` `if` `(s.Length < k) ` ` ` `{ ` ` ` `Console.Write(` `"Not Possible"` `+ ` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// When size of String is equal to k ` ` ` `if` `(s.Length == k) ` ` ` `{ ` ` ` `Console.Write(` `"Possible"` `+ ` `"\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// When size of String is greater than k ` ` ` `// to store the frequencies of the characters ` ` ` `Dictionary<` `int` `, ` ` ` `int` `> freq = ` `new` `Dictionary<` `int` `, ` ` ` `int` `>(); ` ` ` `for` `(` `int` `i = 0; i < s.Length; i++) ` ` ` `if` `(freq.ContainsKey(s[i])) ` ` ` `{ ` ` ` `freq[s[i]] = freq[s[i]] + 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `freq.Add(s[i], 1); ` ` ` `} ` ` ` ` ` `// To store the count of characters ` ` ` `// whose number of occurrences is odd. ` ` ` `int` `count = 0; ` ` ` ` ` `// Iterating over the map ` ` ` `foreach` `(KeyValuePair<` `int` `, ` `int` `> it ` `in` `freq) ` ` ` `{ ` ` ` `if` `(it.Value % 2 == 1) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `if` `(count > k) ` ` ` `Console.Write(` `"No"` `+ ` `"\n"` `); ` ` ` `else` ` ` `Console.Write(` `"Yes"` `+ ` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"poor"` `; ` ` ` `int` `K = 3; ` ` ` `iskPalindromesPossible(str, K); ` ` ` ` ` `str = ` `"geeksforgeeks"` `; ` ` ` `K = 10; ` ` ` `iskPalindromesPossible(str, K); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

Yes Yes

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 the string contains consecutive letters and each letter occurs exactly once
- Find the String having each substring with exactly K distinct characters
- Modify the string such that it contains all vowels at least once
- Count of strings that can be formed from another string using each character at-most once
- Minimum number of operations to move all uppercase characters before all lower case characters
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Remove characters from a String that appears exactly K times
- Min flips of continuous characters to make all characters same in a string
- Find the smallest window in a string containing all characters of another string
- Check whether the frequencies of all the characters in a string are prime or not
- Find the element that appears once in a sorted array
- Count number of substrings with exactly k distinct characters
- Sub-strings having exactly k characters that have ASCII value greater than p
- Minimum length substring with exactly K distinct characters
- Count of substrings of length K with exactly K distinct characters
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum characters to be replaced to make frequency of all characters same
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- String with k distinct characters and no same characters adjacent
- Permutation of a string with maximum number of characters greater than its adjacent characters

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.