<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 minimumBeautifulSubstrings(self, s: str) -> int:
        
        def isBeautiful(string):
            """
            Check if a binary string is 'beautiful', meaning it represents
            a power of 5 and starts with '1'.
            """
            # A beautiful string cannot start with '0'
            if string[0] == "0":
                return False
            
            # Convert binary string to an integer
            num = int(string, 2)
            
            # Check if the number is a power of 5
            while num % 5 == 0:
                num //= 5
            
            # If the number is reduced to 1, it is a power of 5
            return num == 1

        def dfs(string):
            """
            Perform a depth-first search to find the minimum number of beautiful substrings.
            Use memoization to store intermediate results and avoid redundant calculations.
            """
            # If we have already computed the result for this substring, return it
            if string in cache:
                return cache[string]
            
            # If the whole string is beautiful, return 1
            if isBeautiful(string):
                return 1
            
            # Initialize minCount as infinity to find the minimum number of partitions
            minCount = float('inf')
            
            # Try every possible partition of the string
            for i in range(1, len(string)):
                # Split the string into two parts and recursively solve for both
                left = dfs(string[:i])
                right = dfs(string[i:])
                
                # Update minCount with the minimum value found
                if left != float('inf') and right != float('inf'):
                    minCount = min(minCount, left + right)
            
            # Cache the result for this substring
            cache[string] = minCount
            return minCount

        # Memoization dictionary to store the results of intermediate substrings
        cache = {}
        
        # Start the DFS from the full string
        res = dfs(s)
        
        # If res is still infinity, it means no valid partitioning exists
        return res if res != float('inf') else -1

# Example usage:
sol = Solution()
print(sol.minimumBeautifulSubstrings("1011"))  # Expected output: 2
print(sol.minimumBeautifulSubstrings("11110100001001"))  # Expected output: 1
17600