Related Articles

Related Articles

Count lexicographically increasing K-length strings possible from first N alphabets
  • Last Updated : 17 Nov, 2020

Given two positive integers N and K, the task is to find the number of K length strings that can be generated from the first N alphabets such that the characters in the string are sorted lexicographically.

Examples:

Input: N = 5, K = 2
Output: 15
Explanation: All possible strings are {“AA”, “AB”, “AC”, “AD”, “AE”, “BB”, “BC”, “BD”, “BE”, “CC”, “CD”, “CE”, “DD”, “DE”, “EE”}.

Input: N = 7, K = 10
Output: 8008

 

Naive Approach: The simplest approach to solve the problem is to use Recursion and Backtracking to generate all possible arrangements of characters and for each sting, check if the characters follow a lexicographically increasing order or not. Print the count of all such strings. 



Time Complexity: O(KN)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is to use Dynamic Programming as there are overlapping subproblems that can be memoized or tabulated in the recursive calls by using an auxiliary 2D array dp[][] and calculate the value of each state in the bottom-up approach.

dp[i][j] represents the number of ways to arrange “i” length strings with the “j” distinct letters.
dp[i][j] =  dp[i][j – 1] (Choose not to start with first letter) 
                 +  dp[i – 1][j – 1] (Choose first 1 letter in string as first letter) 
                 +  dp[i – 2][j – 1] (Choose first 2 letters in string as first letter) 
                 + …. 
                 + ….               
                 + dp[0][j – 1] (Choose first i letters in string as first letter)
dp[i][j] = Sum of all values of (j-1)th column for “i” rows

Follow the steps below to solve this problem:

  • Initialize an array columnSum[] of size (N+1), where columnSum[i] is sum of all values in column “j” of the array dp[][].
  • Initialize a dp[][] table of size (K + 1)*(N + 1).
  • Initialize dp[0][i] as 1 and subsequently update array columnSum[].
  • Iterate two nested loops over K and using the variable i and j respectively:
    • Store dp[i][j] as columnSum[j – 1].
    • Update columnSum[j] as columnSum[j] + dp[i][j].
  • After the above steps, print the value of dp[K][N] as the resultant number of ways.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count K-length
// strings from first N alphabets
void waysToArrangeKLengthStrings(
    int N, int K)
{
    // To keep track of column sum in dp
    int column_sum[N + 1] = { 0 }, i, j;
 
    // Auxiliary 2d dp array
    int dp[K + 1][N + 1] = { 0 };
 
    // Initialize dp[0][i] = 1 and
    // update the column_sum
    for (i = 0; i <= N; i++) {
        dp[0][i] = 1;
        column_sum[i] = 1;
    }
 
    // Iterate for K times
    for (i = 1; i <= K; i++) {
 
        // Iterate for N times
        for (j = 1; j <= N; j++) {
 
            // dp[i][j]: Stores the number
            // of ways to form i-length
            // strings consisting of j letters
            dp[i][j] += column_sum[j - 1];
 
            // Update the column_sum
            column_sum[j] += dp[i][j];
        }
    }
 
    // Print number of ways to arrange
    // K-length strings with N alphabets
    cout << dp[K][N];
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 5, K = 2;
 
    // Function Call
    waysToArrangeKLengthStrings(N, K);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
import java.util.*;
   
class GFG{
   
// Function to count K-length
// strings from first N alphabets
static void waysToArrangeKLengthStrings(
    int N, int K)
{
     
    // To keep track of column sum in dp
    int[] column_sum = new int[N + 1];
    int i, j;
     
    for(i = 1; i < N + 1; i++)
    {
        column_sum[i] = 0;
    }
  
    // Auxiliary 2d dp array
    int dp[][] = new int[K + 1][N + 1];
     
    for(i = 1; i < K + 1; i++)
    {
        for(j = 1; j < N + 1; j++)
        {
            dp[i][j] = 0;
        }
    }
  
    // Initialize dp[0][i] = 1 and
    // update the column_sum
    for(i = 0; i <= N; i++)
    {
        dp[0][i] = 1;
        column_sum[i] = 1;
    }
  
    // Iterate for K times
    for(i = 1; i <= K; i++)
    {
         
        // Iterate for N times
        for(j = 1; j <= N; j++)
        {
             
            // dp[i][j]: Stores the number
            // of ways to form i-length
            // strings consisting of j letters
            dp[i][j] += column_sum[j - 1];
  
            // Update the column_sum
            column_sum[j] += dp[i][j];
        }
    }
     
    // Print number of ways to arrange
    // K-length strings with N alphabets
    System.out.print(dp[K][N]);
}
   
// Driver Code
public static void main(String[] args)
{
     
    // Given N and K
    int N = 5, K = 2;
  
    // Function Call
    waysToArrangeKLengthStrings(N, K);
}
}
   
// This code is contributed by susmitakundugoaldanga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to count K-length
# strings from first N alphabets
def waysToArrangeKLengthStrings(N, K):
     
    # To keep track of column sum in dp
    column_sum = [0 for i in range(N + 1)]
    i = 0
    j = 0
 
    # Auxiliary 2d dp array
    dp = [[0 for i in range(N + 1)]
             for j in range(K + 1)]
              
    # Initialize dp[0][i] = 1 and
    # update the column_sum
    for i in range(N + 1):
        dp[0][i] = 1
        column_sum[i] = 1
         
    # Iterate for K times
    for i in range(1, K + 1):
         
        # Iterate for N times
        for j in range(1, N + 1):
             
            # dp[i][j]: Stores the number
            # of ways to form i-length
            # strings consisting of j letters
            dp[i][j] += column_sum[j - 1]
 
            # Update the column_sum
            column_sum[j] += dp[i][j]
 
    # Print number of ways to arrange
    # K-length strings with N alphabets
    print(dp[K][N])
 
# Driver Code
if __name__ == '__main__':
     
    # Given N and K
    N = 5
    K = 2
 
    # Function Call
    waysToArrangeKLengthStrings(N, K)
 
# This code is contributed by SURENDRA_GANGWAR

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
    
class GFG{
     
// Function to count K-length
// strings from first N alphabets
static void waysToArrangeKLengthStrings(int N,
                                        int K)
{
   
    // To keep track of column sum in dp
    int[] column_sum = new int[N + 1];
    int i, j;
      
    for(i = 1; i < N + 1; i++)
    {
        column_sum[i] = 0;
    }
   
    // Auxiliary 2d dp array
    int[,] dp = new int[K + 1, N + 1];
      
    for(i = 1; i < K + 1; i++)
    {
        for(j = 1; j < N + 1; j++)
        {
            dp[i, j] = 0;
        }
    }
   
    // Initialize dp[0][i] = 1 and
    // update the column_sum
    for(i = 0; i <= N; i++)
    {
        dp[0, i] = 1;
        column_sum[i] = 1;
    }
   
    // Iterate for K times
    for(i = 1; i <= K; i++)
    {
          
        // Iterate for N times
        for(j = 1; j <= N; j++)
        {
              
            // dp[i][j]: Stores the number
            // of ways to form i-length
            // strings consisting of j letters
            dp[i, j] += column_sum[j - 1];
   
            // Update the column_sum
            column_sum[j] += dp[i, j];
        }
    }
      
    // Print number of ways to arrange
    // K-length strings with N alphabets
    Console.Write(dp[K, N]);
}
  
// Driver Code
public static void Main()
{
   
    // Given N and K
    int N = 5, K = 2;
   
    // Function Call
    waysToArrangeKLengthStrings(N, K);
}
}
 
// This code is contributed by code_hunt

chevron_right


Output: 

15











 

Time Complexity: O(N*K)
Auxiliary Space: O(N*K)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :