Check if any permutation of string is a K times repeated string

Given a string S and an integer K, the task is to check that if any permutation of the string can be formed by K times repeating any other string.

Examples:

Input: S = “abba”, K = 2
Output: Yes
Explanation:
Permutations of given string –
{“aabb”, “abab”, “abba”, “baab”, “baba”, “bbaa”}
As “abab” is repeating string of “ab”+”ab” = “abab”, which is also permutation of string.

Input: S = “abcabd”, K = 2
Output: No
Explanation:
There is no such repeating string in all permutations of the given string.

Approach: The idea is to find the frequency of each character of the string and check that the frequency of the character is a multiple of the given integer K. If the frequency of all characters of the string is divisible by K, then there is a string which is a permutation of the given string and also a K times repeated string.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to check that
// the permutation of the given string
// is K times repeated string
  
#include <bits/stdc++.h>
  
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];
      
    // Intially 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]; 
      
    // Intially 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)]
  
    # Intially 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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]; 
       
    // Intially 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

chevron_right


Output:

No

Performance Analysis:

  • Time Complexity O(N)
  • Auxillary Space: O(123)
  • Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




    My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    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.