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++
// 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:
- Longest sub string of 0's in a binary string which is repeated K times
- Number of subsequences as "ab" in a string repeated K times
- Minimum number of times A has to be repeated such that B is a substring of it
- Largest element in the array that is repeated exactly k times
- Check if a string can be repeated to make another string
- Minimum length of string having all permutation of given string.
- Find the first repeated character in a string
- Queries for characters in a repeated string
- Find the first repeated word in a string
- Remove Duplicate/Repeated words from String
- Count occurrences of a character in a repeated string
- Find repeated character present first in a string
- Smallest length string with repeated replacement of two distinct adjacent
- Minimum steps to delete a string after repeated deletion of palindrome substrings
- Lexicographically n-th permutation of a string
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.



