📌  相关文章
📜  检查字符串的任何排列是否是 K 次重复的字符串

📅  最后修改于: 2021-09-06 06:33:55             🧑  作者: Mango

给定一个字符串S和一个整数K ,任务是检查是否可以通过K次重复任何其他字符串来形成字符串的任何排列。
例子:

方法:想法是找到字符串中每个字符的频率,并检查字符的频率是否是给定整数K的倍数。如果字符串中所有字符的频率都可以被K整除,则存在一个字符串,它是给定字符串的排列,也是 K 次重复的字符串。
下面是上述方法的实现:

C++
// C++ implementation to check that
// the permutation of the given string
// is K times repeated string
 
#include 
 
using namespace std;
 
// Function to check that permutation
// of the given string is a
// K times repeating String
bool repeatingString(string s,
                 int n, int k)
{
    // if length of string is
    // not divisible by K
    if (n % k != 0) {
        return false;
    }
     
    // Frequency Array
    int frequency[123];
     
    // Initially frequency of each
    // character is 0
    for (int i = 0; i < 123; i++) {
        frequency[i] = 0;
    }
     
    // Computing the frequency of
    // each character in the string
    for (int i = 0; i < n; i++) {
        frequency[s[i]]++;
    }
 
    int repeat = n / k;
     
    // Loop to check that frequency of
    // every character of the string
    // is divisible by K
    for (int i = 0; i < 123; i++) {
 
        if (frequency[i] % repeat != 0) {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
int main()
{
    string s = "abcdcba";
    int n = s.size();
    int k = 3;
 
    if (repeatingString(s, n, k)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}


Java
// Java implementation to check that
// the permutation of the given String
// is K times repeated String
class GFG{
 
// Function to check that permutation
// of the given String is a
// K times repeating String
static boolean repeatingString(String s,
                int n, int k)
{
    // if length of String is
    // not divisible by K
    if (n % k != 0) {
        return false;
    }
     
    // Frequency Array
    int []frequency = new int[123];
     
    // Initially frequency of each
    // character is 0
    for (int i = 0; i < 123; i++) {
        frequency[i] = 0;
    }
     
    // Computing the frequency of
    // each character in the String
    for (int i = 0; i < n; i++) {
        frequency[s.charAt(i)]++;
    }
 
    int repeat = n / k;
     
    // Loop to check that frequency of
    // every character of the String
    // is divisible by K
    for (int i = 0; i < 123; i++) {
 
        if (frequency[i] % repeat != 0) {
            return false;
        }
    }
 
    return true;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "abcdcba";
    int n = s.length();
    int k = 3;
 
    if (repeatingString(s, n, k)) {
        System.out.print("Yes" +"\n");
    }
    else {
        System.out.print("No" +"\n");
    }
}
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to check that
# the permutation of the given string
# is K times repeated string
 
# Function to check that permutation
# of the given string is a
# K times repeating String
def repeatingString(s, n, k):
     
    # If length of string is
    # not divisible by K
    if (n % k != 0):
        return False
 
    # Frequency Array
    frequency = [0 for i in range(123)]
 
    # Initially frequency of each
    # character is 0
    for i in range(123):
        frequency[i] = 0
     
    # Computing the frequency of
    # each character in the string
    for i in range(n):
        frequency[s[i]] += 1
 
    repeat = n // k
     
    # Loop to check that frequency of
    # every character of the string
    # is divisible by K
    for i in range(123):
        if (frequency[i] % repeat != 0):
            return False
 
    return True
 
# Driver Code
if __name__ == '__main__':
     
    s = "abcdcba"
    n = len(s)
    k = 3
 
    if (repeatingString(s, n, k)):
        print("Yes")
    else:
        print("No")
         
# This code is contributed by Samarth


C#
// C# implementation to check that
// the permutation of the given String
// is K times repeated String
using System;
 
class GFG{
  
// Function to check that permutation
// of the given String is a
// K times repeating String
static bool repeatingString(String s,
                int n, int k)
{
    // if length of String is
    // not divisible by K
    if (n % k != 0) {
        return false;
    }
      
    // Frequency Array
    int []frequency = new int[123];
      
    // Initially frequency of each
    // character is 0
    for (int i = 0; i < 123; i++) {
        frequency[i] = 0;
    }
      
    // Computing the frequency of
    // each character in the String
    for (int i = 0; i < n; i++) {
        frequency[s[i]]++;
    }
  
    int repeat = n / k;
      
    // Loop to check that frequency of
    // every character of the String
    // is divisible by K
    for (int i = 0; i < 123; i++) {
  
        if (frequency[i] % repeat != 0) {
            return false;
        }
    }
  
    return true;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "abcdcba";
    int n = s.Length;
    int k = 3;
  
    if (repeatingString(s, n, k)) {
        Console.Write("Yes" +"\n");
    }
    else {
        Console.Write("No" +"\n");
    }
}
}
  
// This code is contributed by Rajput-Ji


输出:

No

性能分析:

  • 时间复杂度O(N)
  • 辅助空间: O(123)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live