给定一个字符串S 和一个整数 K。任务是形成一个字符串T ,使得字符串T 是字符串S 的重新排序,使其成为K-Concatenated-String 。如果一个字符串恰好包含某个字符串的K 个副本,则称该字符串为 K-Concatenated-String。
例如,字符串“geekgeek”是由字符串“geek”的2个副本连接而成的2-Concatenated-String。
注意:多个答案是可能的。
例子:
Input : s = “gkeekgee”, k=2
Output: geekgeek
eegkeegk is another possible K-Concatenated-String
Input: s = “abcd”, k=2
Output: Not Possible
方法:要找到 K-Concatenated-string 的有效排序,遍历整个字符串并为字符维护一个频率数组,以保存每个字符在字符串出现的次数。因为,在 K-Concatenated-string 中,字符出现的次数应该可以被 K 整除。如果发现任何字符不在此之后,则该字符串不能以任何方式排序来表示 K-Concatenated-string,否则,在 k-Concatenated-string 的单个副本中,可能正好有(第i个字符的频率/K)个第i个字符的副本。当重复 K 次时,此类单个副本形成有效的 K-Concatenated-string。
下面是上述方法的实现:
C++
// C++ program to form a
// K-Concatenated-String from a given string
#include
using namespace std;
// Function to print the k-concatenated string
string kStringGenerate(string str, int k)
{
// maintain a frequency table
// for lowercase letters
int frequency[26] = { 0 };
int len = str.length();
for (int i = 0; i < len; i++) {
// for each character increment its frequency
// in the frequency array
frequency[str[i] - 'a']++;
}
// stores a single copy of a string,
// which on repeating forms a k-string
string single_copy = "";
// iterate over frequency array
for (int i = 0; i < 26; i++) {
// if the character occurs in the string,
// check if it is divisible by k,
// if not divisible then k-string cant be formed
if (frequency[i] != 0) {
if ((frequency[i] % k) != 0) {
string ans = "Not Possible";
return ans;
}
else {
// ith character occurs (frequency[i]/k) times in a
// single copy of k-string
int total_occurrences = (frequency[i] / k);
for (int j = 0; j < total_occurrences; j++) {
single_copy += char(i + 97);
}
}
}
}
string kString;
// append the single copy formed k times
for (int i = 0; i < k; i++) {
kString += single_copy;
}
return kString;
}
// Driver Code
int main()
{
string str = "gkeekgee";
int K = 2;
string kString = kStringGenerate(str, K);
cout << kString;
return 0;
}
Java
// Java program to form a
// K-Concatenated-String
// from a given string
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
// Function to print
// the k-concatenated string
static String kStringGenerate(String str,
int k)
{
// maintain a frequency table
// for lowercase letters
int frequency[] = new int[26];
Arrays.fill(frequency,0);
int len = str.length();
for (int i = 0; i < len; i++)
{
// for each character
// increment its frequency
// in the frequency array
frequency[str.charAt(i) - 'a']++;
}
// stores a single copy
// of a string, which on
// repeating forms a k-string
String single_copy = "";
// iterate over
// frequency array
for (int i = 0; i < 26; i++)
{
// if the character occurs
// in the string, check if
// it is divisible by k,
// if not divisible then
// k-string cant be formed
if (frequency[i] != 0)
{
if ((frequency[i] % k) != 0)
{
String ans = "Not Possible";
return ans;
}
else
{
// ith character occurs
// (frequency[i]/k) times in
// a single copy of k-string
int total_occurrences = (frequency[i] / k);
for (int j = 0;
j < total_occurrences; j++)
{
single_copy += (char)(i + 97);
}
}
}
}
String kString = "";
// append the single
// copy formed k times
for (int i = 0; i < k; i++)
{
kString += single_copy;
}
return kString;
}
// Driver Code
public static void main(String[] args)
{
String str = "gkeekgee";
int K = 2;
String kString = kStringGenerate(str, K);
System.out.print(kString);
}
}
Python3
# Python 3 program to form a
# K-Concatenated-String from a given string
# Function to print the k-concatenated string
def kStringGenerate(st, k):
# maintain a frequency table
# for lowercase letters
frequency = [0] * 26
length = len(st)
for i in range(length):
# for each character increment its frequency
# in the frequency array
frequency[ord(st[i]) - ord('a')] += 1
# stores a single copy of a string,
# which on repeating forms a k-string
single_copy = ""
# iterate over frequency array
for i in range(26):
# if the character occurs in the string,
# check if it is divisible by k,
# if not divisible then k-string cant be formed
if (frequency[i] != 0):
if ((frequency[i] % k) != 0):
ans = "Not Possible"
return ans
else:
# ith character occurs (frequency[i]/k) times in a
# single copy of k-string
total_occurrences = (frequency[i] // k)
for j in range(total_occurrences):
single_copy += chr(i + 97)
kString = ""
# append the single copy formed k times
for i in range(k):
kString += single_copy
return kString
# Driver Code
if __name__ == "__main__":
st = "gkeekgee"
K = 2
kString = kStringGenerate(st, K)
print(kString)
# This code is contributed by ukasp.
C#
// C# program to form a
// K-Concatenated-String
// from a given string
using System;
class GFG
{
// Function to print
// the k-concatenated string
static String kStringGenerate(String str,
int k)
{
// maintain a frequency table
// for lowercase letters
int []frequency = new int[26];
int len = str.Length;
for (int i = 0; i < len; i++)
{
// for each character
// increment its frequency
// in the frequency array
frequency[str[i]- 'a']++;
}
// stores a single copy
// of a string, which on
// repeating forms a k-string
String single_copy = "";
// iterate over
// frequency array
for (int i = 0; i < 26; i++)
{
// if the character occurs
// in the string, check if
// it is divisible by k,
// if not divisible then
// k-string cant be formed
if (frequency[i] != 0)
{
if ((frequency[i] % k) != 0)
{
String ans = "Not Possible";
return ans;
}
else
{
// ith character occurs
// (frequency[i]/k) times in
// a single copy of k-string
int total_occurrences = (frequency[i] / k);
for (int j = 0;
j < total_occurrences; j++)
{
single_copy += (char)(i + 97);
}
}
}
}
String kString = "";
// append the single
// copy formed k times
for (int i = 0; i < k; i++)
{
kString += single_copy;
}
return kString;
}
// Driver Code
public static void Main(String[] args)
{
String str = "gkeekgee";
int K = 2;
String kString = kStringGenerate(str, K);
Console.Write(kString);
}
}
// This code is contributed by Princi Singh
Javascript
eegkeegk
时间复杂度: O(N),其中 N 是字符串的长度。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。