<p>Given an array of <code>digits</code> which is sorted in <strong>non-decreasing</strong> order. You can write numbers using each <code>digits[i]</code> as many times as we want. For example, if <code>digits = [&#39;1&#39;,&#39;3&#39;,&#39;5&#39;]</code>, we may write numbers such as <code>&#39;13&#39;</code>, <code>&#39;551&#39;</code>, and <code>&#39;1351315&#39;</code>.</p>

<p>Return <em>the number of positive integers that can be generated </em>that are less than or equal to a given integer <code>n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;1&quot;,&quot;3&quot;,&quot;5&quot;,&quot;7&quot;], n = 100
<strong>Output:</strong> 20
<strong>Explanation: </strong>
The 20 numbers that can be written are:
1, 3, 5, 7, 11, 13, 15, 17, 31, 33, 35, 37, 51, 53, 55, 57, 71, 73, 75, 77.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;1&quot;,&quot;4&quot;,&quot;9&quot;], n = 1000000000
<strong>Output:</strong> 29523
<strong>Explanation: </strong>
We can write 3 one digit numbers, 9 two digit numbers, 27 three digit numbers,
81 four digit numbers, 243 five digit numbers, 729 six digit numbers,
2187 seven digit numbers, 6561 eight digit numbers, and 19683 nine digit numbers.
In total, this is 29523 integers that can be written using the digits array.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> digits = [&quot;7&quot;], n = 8
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= digits.length &lt;= 9</code></li>
	<li><code>digits[i].length == 1</code></li>
	<li><code>digits[i]</code> is a digit from&nbsp;<code>&#39;1&#39;</code>&nbsp;to <code>&#39;9&#39;</code>.</li>
	<li>All the values in&nbsp;<code>digits</code> are <strong>unique</strong>.</li>
	<li><code>digits</code> is sorted in&nbsp;<strong>non-decreasing</strong> order.</li>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int dp[11][2][2][2];
    vector<int> a,b;

    map<int,int> mp;
    int rec(int level,int l,int r,int taken,vector<string>& digits){
        //pruning and base case
        if(level == b.size()){
            return 1;
        }
        

        //cache check
        if(dp[level][l][r][taken] != -1) return dp[level][l][r][taken];

        //transitions
        int ans = 0;

        int lo = stoi(digits[0]);
        int hi = stoi(digits.back());

        if(l == 1) lo = a[level];
        if(r == 1) hi = b[level];

        if(taken == 0){
            ans = ans + rec(level+1,0,0,0,digits);
        }

        for(int i = lo;i <= hi;i++){
            int tlo = l;
            int thi = r;
            
            if(mp[i] == 0) continue;

            if(i != lo) tlo = 0;
            if(i != hi) thi = 0;

            ans = ans + rec(level+1,tlo,thi,1,digits);
        }
        //save and return
        return dp[level][l][r][taken] = ans;
    }
    int atMostNGivenDigitSet(vector<string>& digits, int n) {

        memset(dp,-1,sizeof(dp));

        for(int i = 0;i < digits.size();i++){
            mp[stoi(digits[i])]++;
        }


        a.push_back(1);
        int x = n;
        while(x != 0){
            b.push_back(x%10);
            x = x/10;
        }
        x = b.size()-a.size();

        for(int i = 0;i < x;i++) a.push_back(0);
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());

        int ans = rec(0,1,1,0,digits)-1;
        return ans;
    }
};
11300