Check if any permutation of string is a K times repeated string | Java 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 ; if length of String is not divisible by K ; Frequency Array ; Initially frequency of each character is 0 ; Computing the frequency of each character in the String ; Loop to check that frequency of every character of the String is divisible by K ; Driver Code
class GFG { static boolean repeatingString ( String s , int n , int k ) { if ( n % k != 0 ) { return false ; } int [ ] frequency = new int [ 123 ] ; for ( int i = 0 ; i < 123 ; i ++ ) { frequency [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { frequency [ s . charAt ( i ) ] ++ ; } int repeat = n / k ; for ( int i = 0 ; i < 123 ; i ++ ) { if ( frequency [ i ] % repeat != 0 ) { return false ; } } return true ; } 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 " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } }