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 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”.
Below is the implementation of the above approach:
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.
- 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 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.
Improved By : mohit kumar 29