String which when repeated exactly K times gives a permutation of S | Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call
function K_String ( s , k ) { let n = s . length ; let fre = new Array ( 26 ) ; for ( let i = 0 ; i < fre . length ; i ++ ) { fre [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) fre [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; let str = " " ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { let x = Math . floor ( fre [ i ] / k ) ; while ( x != 0 ) { str += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; x -- ; } } else { return " " ; } } return str ; } let s = " " ; let k = 2 ; document . write ( K_String ( s , k ) ) ;