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