String with k distinct characters and no same characters adjacent | Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code
function findString ( n , k ) { let res = " " ; for ( let i = 0 ; i < k ; i ++ ) res = res + String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ; let count = 0 ; for ( let i = 0 ; i < n - k ; i ++ ) { res = res + String . fromCharCode ( ' ' . charCodeAt ( 0 ) + count ) ; count ++ ; if ( count == k ) count = 0 ; } return res ; } let n = 5 , k = 2 ; document . write ( findString ( n , k ) ) ;