Count lexicographically increasing K | Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Given N and K ; Function Call
function waysToArrangeKLengthStrings ( N , K ) { let column_sum = [ ] ; let i , j ; for ( i = 1 ; i < N + 1 ; i ++ ) { column_sum [ i ] = 0 ; } let dp = new Array ( K + 1 ) ; for ( i = 0 ; i < dp . length ; i ++ ) { dp [ i ] = new Array ( 2 ) ; } for ( i = 1 ; i < K + 1 ; i ++ ) { for ( j = 1 ; j < N + 1 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 ] [ i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i ] [ j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i ] [ j ] ; } } document . write ( dp [ K ] [ N ] ) ; } let N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ;