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.
Input: S = “annabelle”, K = 2
All characters of string S can be distributed into “elble” and “anna” which are both palindromic.
Input: S =”abcd”, K = 4
Partition all characters of string as single character.
- 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 “No”.
- Store the frequency of all characters in a Map.
- Count the number of characters having an odd frequency.
- If the count is less than given K, then print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
Time Complexity: O (N).
Auxiliary 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.
- Check if a Palindromic String can be formed by concatenating Substrings of two given Strings
- Longest palindromic String formed using concatenation of given strings in any order
- Print all possible palindromic string formed using any pair of given strings
- Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings
- Check if given string can be formed by two other strings or their permutations
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- All possible strings of any length that can be formed from a given string
- Check if permutation of a given string can be made palindromic by removing at most K characters
- Make palindromic string non-palindromic by rearranging its letters
- Check if a string can be formed from another string by at most X circular clockwise shifts
- Check whether second string can be formed from characters of first string
- Count of strings that can be formed from another string using each character at-most once
- Meta Strings (Check if two strings can become same after a swap in one string)
- Check if the given string of words can be formed from words present in the dictionary
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Count of strings that can be formed using a, b and c under given constraints
- Maximum number of strings that can be formed with given zeros and ones
- 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 email@example.com. 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.