<p>You are given a string <code>s</code> that consists of the digits <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code> and two integers <code>k</code> and <code>minLength</code>.</p>

<p>A partition of <code>s</code> is called <strong>beautiful</strong> if:</p>

<ul>
	<li><code>s</code> is partitioned into <code>k</code> non-intersecting substrings.</li>
	<li>Each substring has a length of <strong>at least</strong> <code>minLength</code>.</li>
	<li>Each substring starts with a <strong>prime</strong> digit and ends with a <strong>non-prime</strong> digit. Prime digits are <code>&#39;2&#39;</code>, <code>&#39;3&#39;</code>, <code>&#39;5&#39;</code>, and <code>&#39;7&#39;</code>, and the rest of the digits are non-prime.</li>
</ul>

<p>Return<em> the number of <strong>beautiful</strong> partitions of </em><code>s</code>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>

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

<pre>
<strong>Input:</strong> s = &quot;23542185131&quot;, k = 3, minLength = 2
<strong>Output:</strong> 3
<strong>Explanation:</strong> There exists three ways to create a beautiful partition:
&quot;2354 | 218 | 5131&quot;
&quot;2354 | 21851 | 31&quot;
&quot;2354218 | 51 | 31&quot;
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;23542185131&quot;, k = 3, minLength = 3
<strong>Output:</strong> 1
<strong>Explanation:</strong> There exists one way to create a beautiful partition: &quot;2354 | 218 | 5131&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;3312958&quot;, k = 3, minLength = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> There exists one way to create a beautiful partition: &quot;331 | 29 | 58&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= k, minLength &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consists of the digits <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>.</li>
</ul>

class Solution:
    def partitions(self, s, i, k, l, at_start, memo):
        mod = (10 ** 9) + 7
        if (i, k, at_start) in memo:
            return memo[(i, k, at_start)]
        
        if i >= len(s):
            return 0
        
        prime_digits = {'2', '3', '5', '7'}
        if at_start:
            if s[i] not in prime_digits:
                memo[(i, k, at_start)] = 0
                return memo[(i, k, at_start)]
            
            if i + l - 1 > len(s) - 1:
                memo[(i, k, at_start)] = 0
                return memo[(i, k, at_start)]
            
            memo[(i, k, at_start)] = self.partitions(s, i + l - 1, k, l, False, memo) % mod
            return memo[(i, k, at_start)]
        else:
            if k == 1:
                return 1 if s[-1] not in prime_digits else 0
            
            if s[i] not in prime_digits:
                memo[(i, k, at_start)] = (
                    self.partitions(s, i+1, k-1, l, True, memo) + self.partitions(s, i+1, k, l, False, memo)) % mod
                
                return memo[(i, k, at_start)]
            else:
                memo[(i, k, at_start)] = self.partitions(s, i+1, k, l, False, memo) % mod
                return memo[(i, k, at_start)]

    def beautifulPartitions(self, s: str, k: int, l: int) -> int:
        memo = {}
        return self.partitions(s, 0, k, l, True, memo)

95100