📜  精确重复K次后得到S排列的字符串

📅  最后修改于: 2021-06-26 14:10:09             🧑  作者: Mango

给定一个整数K和一个小写英语字符的字符串str ,任务是找到一个字符串s ,使得当s精确地重复K次时,它给出一个S的排列。如果不存在这样的字符串,则打印-1
例子:

方法:一种有效的方法是计算给定字符串每个字符的频率。如果任何字符的频率都不能被k整除,则解决方案将不可行,并打印-1 。否则,将每个字符(频率/ k)添加到结果字符串,并在最后打印生成的字符串。
下面是上述方法的实现:

C++
// C++ program to find a string which when repeated
// exactly k times gives a permutation
// of the given string
#include 
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


Javascript


输出:
ab

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。