Given an integer K and a string str of lowercase English characters, the task is to find a string s such that when s is repeated exactly K times, it gives a permutation of S. If no such string exists, print -1.
Examples:
Input: str = “aabb”, k = 2
Output: ab
“ab” when repeated 2 times gives “abab” which is a permutation of “aabb”Input: str = “aabb”, k = 3
Output: -1
Approach: An efficient approach is to count the frequency of each character of the given string. If the frequency of any character is not divisible by k then the solution is not possible and print -1. Otherwise, add every character (frequency / k) times to the resultant string and print the generated string in the end.
Below is the implementation of the above approach:
C++
// C++ program to find a string which when repeated // exactly k times gives a permutation // of the given string #include <bits/stdc++.h> using namespace std; // Function to return a string which when repeated // exactly k times gives a permutation of s string K_String(string s, int k) { // size of string int n = s.size(); // to frequency of each character int fre[26] = { 0 }; // get frequency of each character for (int i = 0; i < n; i++) fre[s[i] - 'a']++; // to store final answer string str = ""; for (int i = 0; i < 26; i++) { // check if frequency is divisible by k if (fre[i] % k == 0) { int x = fre[i] / k; // add to answer while (x--) { str += (char)(i + 'a'); } } // if frequency is not divisible by k else { return "-1"; } } return str; } // Driver code int main() { string s = "aabb"; int k = 2; // function call cout << K_String(s, k); return 0; } |
Java
// Java program to find a string which when repeated // exactly k times gives a permutation // of the given string class GfG { // Function to return a string which when repeated // exactly k times gives a permutation of s static String K_String(String s, int k) { // size of string int n = s.length(); // to frequency of each character int fre[] = new int[26]; // get frequency of each character for (int i = 0; i < n; i++) fre[s.charAt(i) - 'a']++; // to store final answer String str = ""; for (int i = 0; i < 26; i++) { // check if frequency is divisible by k if (fre[i] % k == 0) { int x = fre[i] / k; // add to answer while (x != 0) { str += (char)(i + 'a'); x--; } } // if frequency is not divisible by k else { return "-1"; } } return str; } // Driver code public static void main(String[] args) { String s = "aabb"; int k = 2; // function call System.out.println(K_String(s, k)); } } |
Python 3
# Python 3 program to find a string # which when repeated exactly k times # gives a permutation of the given string # Function to return a string which # when repeated exactly k times gives # a permutation of s def K_String(s, k): # size of string n = len(s) # to frequency of each character fre = [0] * 26 # get frequency of each character for i in range(n): fre[ord(s[i]) - ord('a')] += 1 # to store final answer str = "" for i in range( 26) : # check if frequency is divisible by k if (fre[i] % k == 0) : x = fre[i] // k # add to answer while (x) : str += chr(i + ord('a')) x -= 1 # if frequency is not divisible by k else : return "-1" return str # Driver code if __name__ == "__main__": s = "aabb" k = 2 # function call print( K_String(s, k)) # This code is contributed # by ChitraNayal |
C#
// C# program to find a string which // when repeated exactly k times gives // a permutation of the given string using System; class GFG { // Function to return a string which // when repeated exactly k times gives // a permutation of s static String K_String(String s, int k) { // size of string int n = s.Length ; // to frequency of each character int []fre = new int[26]; // get frequency of each character for (int i = 0; i < n; i++) fre[s[i] - 'a']++; // to store final answer String str = ""; for (int i = 0; i < 26; i++) { // check if frequency is // divisible by k if (fre[i] % k == 0) { int x = fre[i] / k; // add to answer while (x != 0) { str += (char)(i + 'a'); x--; } } // if frequency is not divisible by k else { return "-1"; } } return str; } // Driver code public static void Main(String []args) { String s = "aabb"; int k = 2; // function call Console.WriteLine(K_String(s, k)); } } // This code is contributed by Arnab Kundu |
PHP
<?php // PHP program to find a string which // when repeated exactly k times gives // a permutation of the given string // Function to return a string which // when repeated exactly k times gives // a permutation of s function K_String($s, $k) { // size of string $n = strlen($s); // to frequency of each character $fre = $array = array_fill(0, 26, 0); // get frequency of each character for ($i = 0; $i < $n; $i++) $fre[ord($s[$i]) - ord('a')]++; // to store final answer $str = ""; for ($i = 0; $i < 26; $i++) { // check if frequency is divisible by k if ($fre[$i] % $k == 0) { $x = $fre[$i] / $k; // add to answer while ($x--) { $str .= chr($i + ord('a')); } } // if frequency is not divisible by k else { return "-1"; } } return $str; } // Driver code $s = "aabb"; $k = 2; // function call echo K_String($s, $k); // This code is contributed by Ryuga ?> |
ab
Recommended Posts:
- Check if any permutation of string is a K times repeated string
- Longest sub string of 0's in a binary string which is repeated K times
- Largest element in the array that is repeated exactly k times
- Smallest String consisting of a String S exactly K times as a Substring
- Count of subarrays which contains a given number exactly K times
- Number of subsequences as "ab" in a string repeated K times
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Remove characters from a String that appears exactly K times
- Minimum number of times A has to be repeated such that B is a substring of it
- Number formed after K times repeated addition of smallest divisor of N
- Lexicographically Smallest Permutation of length N such that for exactly K indices, a[i] > a[i] + 1
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Count words that appear exactly two times in an array of words
- Count of Numbers in a Range where digit d occurs exactly K times
- Number of sub-strings that contain the given character exactly k times
- Find Nth smallest number that is divisible by 100 exactly K times
- Check if original Array is retained after performing XOR with M exactly K times
- Lexicographically smallest string which differs from given strings at exactly K indices
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
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.


