# Reorder the given string to form a K-concatenated string

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”.

Examples:

Input : s = “gkeekgee”, k=2
Output: geekgeek
eegkeegk is another possible K-Concatenated-String

Input: s = “abcd”, k=2

Output: Not Possible

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ program to form a ` `// K-Concatenated-String from a given string ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the k-concatenated string ` `string kStringGenerate(string str, ``int` `k) ` `{ ` ` `  `    ``// maintain a frequency table ` `    ``// for lowercase letters ` `    ``int` `frequency = { 0 }; ` ` `  `    ``int` `len = str.length(); ` ` `  `    ``for` `(``int` `i = 0; i < len; i++) { ` ` `  `        ``// for each character increment its frequency ` `        ``// in the frequency array ` `        ``frequency[str[i] - ``'a'``]++; ` `    ``} ` ` `  `    ``// stores a single copy of a string, ` `    ``// which on repeating forms a k-string ` `    ``string single_copy = ``""``; ` ` `  `    ``// iterate over frequency array ` `    ``for` `(``int` `i = 0; i < 26; i++) { ` ` `  `        ``// if the character occurs in the string, ` `        ``// check if it is divisible by k, ` `        ``// if not divisible then k-string cant be formed ` `        ``if` `(frequency[i] != 0) { ` ` `  `            ``if` `((frequency[i] % k) != 0) { ` ` `  `                ``string ans = ``"Not Possible"``; ` `                ``return` `ans; ` `            ``} ` `            ``else` `{ ` ` `  `                ``// ith character occurs (frequency[i]/k) times in a ` `                ``// single copy of k-string ` `                ``int` `total_occurrences = (frequency[i] / k); ` ` `  `                ``for` `(``int` `j = 0; j < total_occurrences; j++) { ` `                    ``single_copy += ``char``(i + 97); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``string kString; ` ` `  `    ``// append the single copy formed k times ` `    ``for` `(``int` `i = 0; i < k; i++) { ` `        ``kString += single_copy; ` `    ``} ` ` `  `    ``return` `kString; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string str = ``"gkeekgee"``; ` `    ``int` `K = 2; ` ` `  `    ``string kString = kStringGenerate(str, K); ` `    ``cout << kString; ` `    ``return` `0; ` `} `

## Java

 `// Java program to form a ` `// K-Concatenated-String  ` `// from a given string ` `import` `java.io.*; ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to print  ` `// the k-concatenated string ` `static` `String kStringGenerate(String str, ` `                              ``int` `k) ` `{ ` ` `  `    ``// maintain a frequency table ` `    ``// for lowercase letters ` `    ``int` `frequency[] = ``new` `int``[``26``]; ` `    ``Arrays.fill(frequency,``0``); ` `    ``int` `len = str.length(); ` ` `  `    ``for` `(``int` `i = ``0``; i < len; i++)  ` `    ``{ ` ` `  `        ``// for each character  ` `        ``// increment its frequency ` `        ``// in the frequency array ` `        ``frequency[str.charAt(i) - ``'a'``]++; ` `    ``} ` ` `  `    ``// stores a single copy  ` `    ``// of a string, which on  ` `    ``// repeating forms a k-string ` `    ``String single_copy = ``""``; ` ` `  `    ``// iterate over  ` `    ``// frequency array ` `    ``for` `(``int` `i = ``0``; i < ``26``; i++)  ` `    ``{ ` ` `  `        ``// if the character occurs  ` `        ``// in the string, check if  ` `        ``// it is divisible by k, ` `        ``// if not divisible then ` `        ``// k-string cant be formed ` `        ``if` `(frequency[i] != ``0``)  ` `        ``{ ` ` `  `            ``if` `((frequency[i] % k) != ``0``) ` `            ``{ ` `                ``String ans = ``"Not Possible"``; ` `                ``return` `ans; ` `            ``} ` `            ``else` `            ``{ ` ` `  `                ``// ith character occurs  ` `                ``// (frequency[i]/k) times in  ` `                ``// a single copy of k-string ` `                ``int` `total_occurrences = (frequency[i] / k); ` ` `  `                ``for` `(``int` `j = ``0``;  ` `                         ``j < total_occurrences; j++)  ` `                ``{ ` `                    ``single_copy += (``char``)(i + ``97``); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``String kString = ``""``; ` ` `  `    ``// append the single ` `    ``// copy formed k times ` `    ``for` `(``int` `i = ``0``; i < k; i++)  ` `    ``{ ` `        ``kString += single_copy; ` `    ``} ` ` `  `    ``return` `kString; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String str = ``"gkeekgee"``; ` `    ``int` `K = ``2``; ` `     `  `    ``String kString = kStringGenerate(str, K); ` `    ``System.out.print(kString); ` `} ` `} `

## C#

 `// C# program to form a ` `// K-Concatenated-String  ` `// from a given string ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to print  ` `// the k-concatenated string ` `static` `String kStringGenerate(String str, ` `                            ``int` `k) ` `{ ` ` `  `    ``// maintain a frequency table ` `    ``// for lowercase letters ` `    ``int` `[]frequency = ``new` `int``; ` `    ``int` `len = str.Length; ` ` `  `    ``for` `(``int` `i = 0; i < len; i++)  ` `    ``{ ` ` `  `        ``// for each character  ` `        ``// increment its frequency ` `        ``// in the frequency array ` `        ``frequency[str[i]- ``'a'``]++; ` `    ``} ` ` `  `    ``// stores a single copy  ` `    ``// of a string, which on  ` `    ``// repeating forms a k-string ` `    ``String single_copy = ``""``; ` ` `  `    ``// iterate over  ` `    ``// frequency array ` `    ``for` `(``int` `i = 0; i < 26; i++)  ` `    ``{ ` ` `  `        ``// if the character occurs  ` `        ``// in the string, check if  ` `        ``// it is divisible by k, ` `        ``// if not divisible then ` `        ``// k-string cant be formed ` `        ``if` `(frequency[i] != 0)  ` `        ``{ ` ` `  `            ``if` `((frequency[i] % k) != 0) ` `            ``{ ` `                ``String ans = ``"Not Possible"``; ` `                ``return` `ans; ` `            ``} ` `            ``else` `            ``{ ` ` `  `                ``// ith character occurs  ` `                ``// (frequency[i]/k) times in  ` `                ``// a single copy of k-string ` `                ``int` `total_occurrences = (frequency[i] / k); ` ` `  `                ``for` `(``int` `j = 0;  ` `                        ``j < total_occurrences; j++)  ` `                ``{ ` `                    ``single_copy += (``char``)(i + 97); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``String kString = ``""``; ` ` `  `    ``// append the single ` `    ``// copy formed k times ` `    ``for` `(``int` `i = 0; i < k; i++)  ` `    ``{ ` `        ``kString += single_copy; ` `    ``} ` ` `  `    ``return` `kString; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String str = ``"gkeekgee"``; ` `    ``int` `K = 2; ` `     `  `    ``String kString = kStringGenerate(str, K); ` `    ``Console.Write(kString); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```eegkeegk
```

Time Complexity: O(N), where N is the length of the string.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : princi singh

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.