# Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13

Given a string str of length N. The task is to find the number of integers obtained by replacing ‘?’ with any digit such that the formed integer gives remainder 5 when it is divided by 13.

Numbers can also begin with zero. The answer can be very large, so, output answer modulo 109 + 7.

Examples:

Input: str = “?44”
Output: 1
Only possible number is 044

Input: str = “7?4”
Output: 0

Input: str = “8?3?4233?4?”
Output: 770

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Let dp[i][j] be the number of ways to create an i-digit number consistent with the first i digits of the given pattern and congruent to j modulo 13. As our base case, dp[i]=0 for i from 1 to 12, and dp=1 (as our length-zero number has value zero and thus is zero mod 13.)

Notice that appending a digit k to the end of a number that’s j mod 13 gives a number that’s congruent to 10j+k mod 13. We use this fact to perform our transitions. For every state, dp[i][j] with i < N, iterate over the possible values of k. (If s[i]=’?’, there will be ten choices for k, and otherwise, there will only be one choice.) Then, we add dp[i][j] to dp[i+1][(10j+k)%13].

To get our final answer, we can simply print dp[N].

Below is the implementation of the above approach :

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define MOD (int)(1e9 + 7) ` ` `  `// Function to find the count of integers ` `// obtained by replacing '?' in a given ` `// string such that formed integer ` `// gives remainder 5 when it is divided by 13 ` `int` `modulo_13(string s, ``int` `n) ` `{ ` `    ``long` `long` `dp[n + 1] = { { 0 } }; ` ` `  `    ``// Initialise ` `    ``dp = 1; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < 10; j++) { ` `            ``int` `nxt = s[i] - ``'0'``; ` ` `  `            ``// Place digit j at ? position ` `            ``if` `(s[i] == ``'?'``) ` `                ``nxt = j; ` ` `  `            ``// Get the remainder ` `            ``for` `(``int` `k = 0; k < 13; k++) { ` `                ``int` `rem = (10 * k + nxt) % 13; ` `                ``dp[i + 1][rem] += dp[i][k]; ` `                ``dp[i + 1][rem] %= MOD; ` `            ``} ` `            ``if` `(s[i] != ``'?'``) ` `                ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `(``int``)dp[n]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"?44"``; ` `    ``int` `n = s.size(); ` ` `  `    ``cout << modulo_13(s, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` `static` `int` `MOD = (``int``)(1e9 + ``7``); ` ` `  `// Function to find the count of integers ` `// obtained by replacing '?' in a given ` `// string such that formed integer ` `// gives remainder 5 when it is divided by 13 ` `static` `int` `modulo_13(String s, ``int` `n) ` `{ ` `    ``long` `[][]dp = ``new` `long``[n + ``1``][``13``]; ` ` `  `    ``// Initialise ` `    ``dp[``0``][``0``] = ``1``; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < ``10``; j++)  ` `        ``{ ` `            ``int` `nxt = s.charAt(i) - ``'0'``; ` ` `  `            ``// Place digit j at ? position ` `            ``if` `(s.charAt(i) == ``'?'``) ` `                ``nxt = j; ` ` `  `            ``// Get the remainder ` `            ``for` `(``int` `k = ``0``; k < ``13``; k++) ` `            ``{ ` `                ``int` `rem = (``10` `* k + nxt) % ``13``; ` `                ``dp[i + ``1``][rem] += dp[i][k]; ` `                ``dp[i + ``1``][rem] %= MOD; ` `            ``} ` `            ``if` `(s.charAt(i) != ``'?'``) ` `                ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `(``int``)dp[n][``5``]; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args)  ` `{ ` `    ``String s = ``"?44"``; ` `    ``int` `n = s.length(); ` ` `  `    ``System.out.println(modulo_13(s, n)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach  ` `import` `numpy as np ` ` `  `MOD ``=` `(``int``)(``1e9` `+` `7``)  ` ` `  `# Function to find the count of integers  ` `# obtained by replacing '?' in a given  ` `# string such that formed integer  ` `# gives remainder 5 when it is divided by 13  ` `def` `modulo_13(s, n) : ` `     `  `    ``dp ``=` `np.zeros((n ``+` `1``, ``13``)); ` `     `  `    ``# Initialise  ` `    ``dp[``0``][``0``] ``=` `1``;  ` ` `  `    ``for` `i ``in` `range``(n) :  ` `        ``for` `j ``in` `range``(``10``) : ` `            ``nxt ``=` `ord``(s[i]) ``-` `ord``(``'0'``);  ` ` `  `            ``# Place digit j at ? position  ` `            ``if` `(s[i] ``=``=` `'?'``) : ` `                ``nxt ``=` `j;  ` ` `  `            ``# Get the remainder  ` `            ``for` `k ``in` `range``(``13``) : ` `                ``rem ``=` `(``10` `*` `k ``+` `nxt) ``%` `13``;  ` `                ``dp[i ``+` `1``][rem] ``+``=` `dp[i][k];  ` `                ``dp[i ``+` `1``][rem] ``%``=` `MOD;  ` `         `  `            ``if` `(s[i] !``=` `'?'``) : ` `                ``break``;  ` ` `  `    ``# Return the required answer  ` `    ``return` `int``(dp[n][``5``]);  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` `    ``s ``=` `"?44"``;  ` `    ``n ``=` `len``(s);  ` ` `  `    ``print``(modulo_13(s, n));  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `                     `  `class` `GFG ` `{ ` ` `  `static` `int` `MOD = (``int``)(1e9 + 7); ` ` `  `// Function to find the count of integers ` `// obtained by replacing '?' in a given ` `// string such that formed integer ` `// gives remainder 5 when it is divided by 13 ` `static` `int` `modulo_13(String s, ``int` `n) ` `{ ` `    ``long` `[,]dp = ``new` `long``[n + 1, 13]; ` ` `  `    ``// Initialise ` `    ``dp[0, 0] = 1; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``for` `(``int` `j = 0; j < 10; j++)  ` `        ``{ ` `            ``int` `nxt = s[i] - ``'0'``; ` ` `  `            ``// Place digit j at ? position ` `            ``if` `(s[i] == ``'?'``) ` `                ``nxt = j; ` ` `  `            ``// Get the remainder ` `            ``for` `(``int` `k = 0; k < 13; k++) ` `            ``{ ` `                ``int` `rem = (10 * k + nxt) % 13; ` `                ``dp[i + 1, rem] += dp[i, k]; ` `                ``dp[i + 1, rem] %= MOD; ` `            ``} ` `            ``if` `(s[i] != ``'?'``) ` `                ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Return the required answer ` `    ``return` `(``int``)dp[n,5]; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args)  ` `{ ` `    ``String s = ``"?44"``; ` `    ``int` `n = s.Length; ` ` `  `    ``Console.WriteLine(modulo_13(s, n)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.