Given a string S and an integer K. The task is to form a string T such that the string T is a reordering of the string S in a way that it is a K-Concatenated-String. A string is said to be a K-Concatenated-String if it contains exactly K copies of some string.
For example, the string “geekgeek” is a 2-Concatenated-String formed by concatenating 2 copies of the string “geek”.
Note: Multiple answers are possible.
Input : s = “gkeekgee”, k=2
eegkeegk is another possible K-Concatenated-String
Input: s = “abcd”, k=2
Output: Not Possible
Approach: To find a valid ordering that is a K-Concatenated-string, iterate through the entire string and maintain a frequency array for the characters to hold the number of times each character occurs in a string. Since, in a K-Concatenated-string, the number of times a character occurs should be divisible by K. If any character is found not following this, then the string cant be ordered in any way to represent a K-Concatenated-string, else there can be exactly (Frequency of ith character / K) copies of the ith character in a single copy of the k-Concatenated-string. Such single copies when repeated K times form a valid K-Concatenated-string.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
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 characters of a given string can be rearranged to form a palindrome
- Check if characters of a given string can be used to form any N equal strings
- Minimal moves to form a string by adding characters or appending string itself
- Check if the characters in a string form a Palindrome in O(1) extra space
- Check if a string can be rearranged to form special palindrome
- Convert an array to reduced form | Set 1 (Simple and Hashing)
- Minimum insertions to form a palindrome with permutations allowed
- Maximum length subsequence possible of the form R^N K^N
- Print all triplets in sorted array that form AP
- Probability that the pieces of a broken stick form a n sided polygon
- Count of elements which cannot form any pair whose sum is power of 2
- Generate a String from given Strings P and Q based on the given conditions
- Python | Check if a given string is binary string or not
- Count occurrences of a string that can be constructed from another given string
- Rearrange given string to maximize the occurrence of string t
- Construct a string that has exactly K subsequences from given string
- Length of smallest substring of a given string which contains another string as subsequence
- Check if given permutation of 1 to N is feasible using given operations
- Minimize count of given operations required to make two given strings permutations of each other
- Check if concatenation of any permutation of given list of arrays generates the given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : princi singh