<p>Given a binary string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that each substring is <strong>beautiful</strong>.</p>

<p>A string is <strong>beautiful</strong> if:</p>

<ul>
	<li>It doesn&#39;t contain leading zeros.</li>
	<li>It&#39;s the <strong>binary</strong> representation of a number that is a power of <code>5</code>.</li>
</ul>

<p>Return <em>the <strong>minimum</strong> number of substrings in such partition. </em>If it is impossible to partition the string <code>s</code> into beautiful substrings,&nbsp;return <code>-1</code>.</p>

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

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

<pre>
<strong>Input:</strong> s = &quot;1011&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> We can paritition the given string into [&quot;101&quot;, &quot;1&quot;].
- The string &quot;101&quot; does not contain leading zeros and is the binary representation of integer 5<sup>1</sup> = 5.
- The string &quot;1&quot; does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
It can be shown that 2 is the minimum number of beautiful substrings that s can be partitioned into.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;111&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can paritition the given string into [&quot;1&quot;, &quot;1&quot;, &quot;1&quot;].
- The string &quot;1&quot; does not contain leading zeros and is the binary representation of integer 5<sup>0</sup> = 1.
It can be shown that 3 is the minimum number of beautiful substrings that s can be partitioned into.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;0&quot;
<strong>Output:</strong> -1
<strong>Explanation:</strong> We can not partition the given string into beautiful substrings.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 15</code></li>
	<li><code>s[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

class Solution:
    def isFive(self, num: int) -> bool:
        while num > 1:
            if num % 5 != 0:
                return False
            num //= 5
        return num == 1

    def isSubstringBeautiful(self, i: int, j: int, s: str) -> bool:
        str = s[i:j + 1]
        k = 0
        while k < len(str):
            if str[k] == '0':
                k += 1
                return False
            else:
                break
        ans = 0
        for m in range(len(str) - 1, -1, -1):
            if str[m] == '1':
                ans += 2 ** (len(str) - 1 - m)
        return self.isFive(ans)

    def func(self, idx: int, s: str, dp: List[int]) -> int:
        if idx == len(s):
            return 0
        if dp[idx] != -1:
            return dp[idx]
        min_part = int(1e9)
        for j in range(idx, len(s)):
            if self.isSubstringBeautiful(idx, j, s):
                cost = 1 + self.func(j + 1, s, dp)
                min_part = min(cost, min_part)
        dp[idx] = min_part
        return min_part

    def minimumBeautifulSubstrings(self, s: str) -> int:
        dp = [-1] * (len(s) + 1)
        x = self.func(0, s, dp)
        return -1 if x == int(1e9) else x
52