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
< ? php function K_String ( $ s , $ k ) { $ n = strlen ( $ s ) ; $ fre = $ array = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ fre [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ str = " " ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ fre [ $ i ] % $ k == 0 ) { $ x = $ fre [ $ i ] / $ k ; while ( $ x -- ) { $ str . = chr ( $ i + ord ( ' a ' ) ) ; } } else { return " - 1" ; } } return $ str ; } $ s = " aabb " ; $ k = 2 ; echo K_String ( $ s , $ k ) ; ? >