Generate a string of size N whose each substring of size M has exactly K distinct characters | Function to generate the String ; Declare empty string ; counter for M ; counter for K ; Loop to generate string size of N ; Generating K distinct letters one by one ; After generating b distinct letters , append rest a - b letters as ' a ' ; Reset the counter value and repeat the process ; return final result string ; Driver code
function generateString ( N , M , K ) { let s = " " ; let cnt1 = 0 ; let cnt2 = 0 ; for ( let i = 0 ; i < N ; i ++ ) { cnt1 ++ ; cnt2 ++ ; if ( cnt1 <= M ) { if ( cnt2 <= K ) { s = s + String . fromCharCode ( 96 + cnt1 ) ; } else { s = s + ' ' ; } } else { cnt1 = 1 ; cnt2 = 1 ; s = s + ' ' ; } } return s ; } let N = 7 , M = 5 , K = 3 ; document . write ( generateString ( N , M , K ) )