Sum of Digits of the Good Strings
Last Updated :
07 Mar, 2022
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:
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:
- dp[i][j]: Sum of the digits of all possible good strings of length i that end with j.
- 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++
#include <bits/stdc++.h>
using namespace std;
#define DIGITS 10
#define MAX 10000
#define MOD 1000000007
long dp[MAX][DIGITS], cnt[MAX][DIGITS];
void precompute()
{
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++) {
if (j != k) {
dp[i][j] = dp[i][j]
+ (dp[i - 1][k] + (cnt[i - 1][k] * j) % MOD)
% MOD;
dp[i][j] %= MOD;
cnt[i][j] += cnt[i - 1][k];
cnt[i][j] %= MOD;
}
}
}
}
}
int main()
{
long long int x = 6, y = 4;
precompute();
cout << dp[x][y];
return 0;
}
|
Java
class GFG
{
final static int DIGITS = 10 ;
final static int MAX = 10000 ;
final static int MOD = 1000000007 ;
static int dp[][] = new int [MAX][DIGITS];
static int cnt[][] = new int [MAX][DIGITS];
static void precompute()
{
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++)
{
if (j != k)
{
dp[i][j] = dp[i][j] + (dp[i - 1 ][k] +
(cnt[i - 1 ][k] * j) % MOD)
% MOD;
dp[i][j] %= MOD;
cnt[i][j] += cnt[i - 1 ][k];
cnt[i][j] %= MOD;
}
}
}
}
}
public static void main (String[] args)
{
int x = 6 , y = 4 ;
precompute();
System.out.println(dp[x][y]);
}
}
|
Python3
DIGITS = 10 ;
MAX = 10000 ;
MOD = 1000000007 ;
dp = [[ 0 for i in range (DIGITS)]
for i in range ( MAX )];
cnt = [[ 0 for i in range (DIGITS)]
for i in range ( MAX )];
def precompute():
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):
if (j ! = k):
dp[i][j] = dp[i][j] + (dp[i - 1 ][k] + \
(cnt[i - 1 ][k] * j) % MOD) % MOD;
dp[i][j] % = MOD;
cnt[i][j] + = cnt[i - 1 ][k];
cnt[i][j] % = MOD;
x = 6 ; y = 4 ;
precompute();
print (dp[x][y]);
|
C#
using System;
class GFG
{
readonly static int DIGITS = 10;
readonly static int MAX = 10000;
readonly static int MOD = 1000000007;
static int [,]dp = new int [MAX, DIGITS];
static int [,]cnt = new int [MAX, DIGITS];
static void precompute()
{
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++)
{
if (j != k)
{
dp[i, j] = dp[i, j] + (dp[i - 1, k] +
(cnt[i - 1, k] * j) % MOD)
% MOD;
dp[i, j] %= MOD;
cnt[i, j] += cnt[i - 1, k];
cnt[i, j] %= MOD;
}
}
}
}
}
public static void Main (String[] args)
{
int x = 6, y = 4;
precompute();
Console.WriteLine(dp[x,y]);
}
}
|
Javascript
<script>
var DIGITS = 10
var MAX = 10000
var MOD = 1000000007
var dp = Array.from(Array(MAX), ()=> Array(DIGITS).fill(0));
var cnt = Array.from(Array(MAX), ()=> Array(DIGITS).fill(0));
function precompute()
{
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++) {
if (j != k) {
dp[i][j] = dp[i][j]
+ (dp[i - 1][k] +
(cnt[i - 1][k] * j) % MOD)% MOD;
dp[i][j] %= MOD;
cnt[i][j] += cnt[i - 1][k];
cnt[i][j] %= MOD;
}
}
}
}
}
var x = 6, y = 4;
precompute();
document.write( dp[x][y]);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(105)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...