Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Sum of Digits of the Good Strings

  • Last Updated : 25 May, 2021

A string is called good if it is made with only digits 0 to 9 and adjacent elements are different. The task is to find the sum of the digits of all possible good strings of length X that end with the given digit Y. The answer could be large, so print the answer modulo 109 + 7.

Examples:  

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: X = 2, Y = 2 
Output: 61 
All possible strings of length 2 that end with 2 are: 
02, 12, 32, 42, 52, 62, 72, 82, 92. 
Now, ((0 + 2) + (1 + 2) + (3 + 2) + (4 + 2) + (5 + 2) 
+ (6 + 2) + (7 + 2) + (8 + 2) + (9 + 2)) = 61



Input: X = 6, Y = 4 
Output: 1567751 

Approach: This problem can be solved by using dynamic programming. Let’s define the following states:  

  1. dp[i][j]: Sum of the digits of all possible good strings of length i that end with j.
  2. cnt[i][j]: Count the good strings of length i that end with j.

The value of the previous state will have to be used to compute the value for the current state as the adjacent digits have to be compared to whether they are equal or not. Now, the recurrence relation will be:  

dp[i][j] = dp[i][j] + dp[i – 1][k] + cnt[i – 1][k] * j

 Here, dp[i – 1][k] is the sum of the digits of good strings of length (i – 1) that end with k and k != j
cnt[i -1][k] is the count of good strings of length (i – 1) that end with k and k != j
So for position i, (cnt(i – 1)[k] * j) has to be added as j is being put at index i and the count of possible strings that have length (i – 1) is cnt[i – 1][k].
Below is the implementation of the above approach:  

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
#define DIGITS 10
#define MAX 10000
#define MOD 1000000007
 
// To store the states of the dp
long dp[MAX][DIGITS], cnt[MAX][DIGITS];
 
// Function to fill the dp table
void precompute()
{
 
    // dp[i][j] : Sum of the digits of all
    // possible good strings of length
    // i that end with j
    // cnt[i][j] : Count of the good strings
    // of length i that end with j
 
    // Sum of digits of the string of length
    // 1 is i as i is only number in that string
    // and count of good strings of length 1
    // that end with i is also 1
    for (int i = 0; i < DIGITS; i++)
        dp[1][i] = i, cnt[1][i] = 1;
 
    for (int i = 2; i < MAX; i++) {
        for (int j = 0; j < DIGITS; j++) {
            for (int k = 0; k < DIGITS; k++) {
 
                // Adjacent digits are different
                if (j != k) {
                    dp[i][j] = dp[i][j]
                               + (dp[i - 1][k] + (cnt[i - 1][k] * j) % MOD)
                                     % MOD;
                    dp[i][j] %= MOD;
 
                    // Increment the count as digit at
                    // (i - 1)'th index is k and count
                    // of good strings is equal to this
                    // because at the end of the strings of
                    // length (i - 1) we are just
                    // putting digit j as the last digit
                    cnt[i][j] += cnt[i - 1][k];
                    cnt[i][j] %= MOD;
                }
            }
        }
    }
}
 
// Driver code
int main()
{
    long long int x = 6, y = 4;
 
    precompute();
 
    cout << dp[x][y];
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
    final static int DIGITS = 10;
    final static int MAX = 10000;
    final static int MOD = 1000000007;
     
    // To store the states of the dp
    static int dp[][] = new int[MAX][DIGITS];
    static int cnt[][] = new int[MAX][DIGITS];
     
    // Function to fill the dp table
    static void precompute()
    {
     
        // dp[i][j] : Sum of the digits of all
        // possible good strings of length
        // i that end with j
        // cnt[i][j] : Count of the good strings
        // of length i that end with j
     
        // Sum of digits of the string of length
        // 1 is i as i is only number in that string
        // and count of good strings of length 1
        // that end with i is also 1
        for (int i = 0; i < DIGITS; i++)
        {
            dp[1][i] = i;
            cnt[1][i] = 1;
        }
     
        for (int i = 2; i < MAX; i++)
        {
            for (int j = 0; j < DIGITS; j++)
            {
                for (int k = 0; k < DIGITS; k++)
                {
     
                    // Adjacent digits are different
                    if (j != k)
                    {
                        dp[i][j] = dp[i][j] + (dp[i - 1][k] +
                                             (cnt[i - 1][k] * j) % MOD)
                                                                 % MOD;
                        dp[i][j] %= MOD;
     
                        // Increment the count as digit at
                        // (i - 1)'th index is k and count
                        // of good strings is equal to this
                        // because at the end of the strings of
                        // length (i - 1) we are just
                        // putting digit j as the last digit
                        cnt[i][j] += cnt[i - 1][k];
                        cnt[i][j] %= MOD;
                    }
                }
            }
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int x = 6, y = 4;
     
        precompute();
     
        System.out.println(dp[x][y]);
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python3 implementation of the approach
DIGITS = 10;
MAX = 10000;
MOD = 1000000007;
 
# To store the states of the dp
dp = [[0 for i in range(DIGITS)]
         for i in range(MAX)];
cnt = [[0 for i in range(DIGITS)]
          for i in range(MAX)];
 
# Function to fill the dp table
def precompute():
 
    # dp[i][j] : Sum of the digits of all
    # possible good strings of length
    # i that end with j
    # cnt[i][j] : Count of the good strings
    # of length i that end with j
 
    # Sum of digits of the string of length
    # 1 is i as i is only number in that string
    # and count of good strings of length 1
    # that end with i is also 1
    for i in range(DIGITS):
     
        dp[1][i] = i;
        cnt[1][i] = 1;
     
    for i in range(2, MAX):
        for j in range(DIGITS):
            for k in range(DIGITS):
                 
                # Adjacent digits are different
                if (j != k):
                 
                    dp[i][j] = dp[i][j] + (dp[i - 1][k] +\
                                         (cnt[i - 1][k] * j) % MOD) % MOD;
                    dp[i][j] %= MOD;
 
                    # Increment the count as digit at
                    # (i - 1)'th index is k and count
                    # of good strings is equal to this
                    # because at the end of the strings of
                    # length (i - 1) we are just
                    # putting digit j as the last digit
                    cnt[i][j] += cnt[i - 1][k];
                    cnt[i][j] %= MOD;
 
# Driver code
x = 6; y = 4;
 
precompute();
 
print(dp[x][y]);
 
# This code is contributed by 29AjayKumar

C#




// C# implementation of the approach
using System;
     
class GFG
{
    readonly static int DIGITS = 10;
    readonly static int MAX = 10000;
    readonly static int MOD = 1000000007;
     
    // To store the states of the dp
    static int [,]dp = new int[MAX, DIGITS];
    static int [,]cnt = new int[MAX, DIGITS];
     
    // Function to fill the dp table
    static void precompute()
    {
     
        // dp[i][j] : Sum of the digits of all
        // possible good strings of length
        // i that end with j
        // cnt[i][j] : Count of the good strings
        // of length i that end with j
     
        // Sum of digits of the string of length
        // 1 is i as i is only number in that string
        // and count of good strings of length 1
        // that end with i is also 1
        for (int i = 0; i < DIGITS; i++)
        {
            dp[1, i] = i;
            cnt[1, i] = 1;
        }
     
        for (int i = 2; i < MAX; i++)
        {
            for (int j = 0; j < DIGITS; j++)
            {
                for (int k = 0; k < DIGITS; k++)
                {
     
                    // Adjacent digits are different
                    if (j != k)
                    {
                        dp[i, j] = dp[i, j] + (dp[i - 1, k] +
                                             (cnt[i - 1, k] * j) % MOD)
                                                                 % MOD;
                        dp[i, j] %= MOD;
     
                        // Increment the count as digit at
                        // (i - 1)'th index is k and count
                        // of good strings is equal to this
                        // because at the end of the strings of
                        // length (i - 1) we are just
                        // putting digit j as the last digit
                        cnt[i, j] += cnt[i - 1, k];
                        cnt[i, j] %= MOD;
                    }
                }
            }
        }
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int x = 6, y = 4;
     
        precompute();
     
        Console.WriteLine(dp[x,y]);
    }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
 
// Javascript implementation of the approach
 
var DIGITS = 10
var MAX = 10000
var MOD = 1000000007
 
// To store the states of the dp
var dp = Array.from(Array(MAX), ()=> Array(DIGITS).fill(0));
var cnt = Array.from(Array(MAX), ()=> Array(DIGITS).fill(0));
 
// Function to fill the dp table
function precompute()
{
 
    // dp[i][j] : Sum of the digits of all
    // possible good strings of length
    // i that end with j
    // cnt[i][j] : Count of the good strings
    // of length i that end with j
 
    // Sum of digits of the string of length
    // 1 is i as i is only number in that string
    // and count of good strings of length 1
    // that end with i is also 1
    for (var i = 0; i < DIGITS; i++)
        dp[1][i] = i, cnt[1][i] = 1;
 
    for (var i = 2; i < MAX; i++) {
        for (var j = 0; j < DIGITS; j++) {
            for (var k = 0; k < DIGITS; k++) {
 
                // Adjacent digits are different
                if (j != k) {
                    dp[i][j] = dp[i][j]
                               + (dp[i - 1][k] +
                                  (cnt[i - 1][k] * j) % MOD)% MOD;
                    dp[i][j] %= MOD;
 
                    // Increment the count as digit at
                    // (i - 1)'th index is k and count
                    // of good strings is equal to this
                    // because at the end of the strings of
                    // length (i - 1) we are just
                    // putting digit j as the last digit
                    cnt[i][j] += cnt[i - 1][k];
                    cnt[i][j] %= MOD;
                }
            }
        }
    }
}
 
// Driver code
var x = 6, y = 4;
precompute();
document.write( dp[x][y]);
 
 
</script>
Output: 
1567751

 

Time Complexity: O(N)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!