String which when repeated exactly K times gives a permutation of S

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)); 
  
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

ab


My Personal Notes arrow_drop_up

Coder Machine Learner Social Activist Vocalist

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.