# Solving Leetcode Interviews in Seconds with AI: Number of Beautiful Partitions


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2478" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> You are given a string s that consists of the digits '1' to '9' and two integers k and minLength. A partition of s is called beautiful if:  s is partitioned into k non-intersecting substrings. Each substring has a length of at least minLength. Each substring starts with a prime digit and ends with a non-prime digit. Prime digits are '2', '3', '5', and '7', and the rest of the digits are non-prime.  Return the number of beautiful partitions of s. Since the answer may be very large, return it modulo 109 + 7. A substring is a contiguous sequence of characters within a string.   Example 1:  Input: s = "23542185131", k = 3, minLength = 2 Output: 3 Explanation: There exists three ways to create a beautiful partition: "2354 | 218 | 5131" "2354 | 21851 | 31" "2354218 | 51 | 31"  Example 2:  Input: s = "23542185131", k = 3, minLength = 3 Output: 1 Explanation: There exists one way to create a beautiful partition: "2354 | 218 | 5131".  Example 3:  Input: s = "3312958", k = 3, minLength = 1 Output: 1 Explanation: There exists one way to create a beautiful partition: "331 | 29 | 58".    Constraints:  1 <= k, minLength <= s.length <= 1000 s consists of the digits '1' to '9'.  

	# Explanation
	Here's the breakdown of the solution:

*   **Dynamic Programming:** Use dynamic programming to store and reuse the number of beautiful partitions for substrings ending at different indices.
*   **State Definition:** `dp[i][j]` represents the number of beautiful partitions of the substring `s[0...i]` into `j` parts.
*   **Base Case and Transitions:** Initialize `dp[0][0]` to 1 if the first partition can be made, and then iterate through possible partition points to build up the `dp` table.

*   **Runtime Complexity:** O(n\*k\*n) , **Storage Complexity:** O(n\*k)

	
	# Code
	```python
	def beautifulPartitions(s: str, k: int, minLength: int) -> int:
    n = len(s)
    MOD = 10**9 + 7

    def is_prime(digit):
        return digit in ('2', '3', '5', '7')

    if not is_prime(s[0]):
        return 0
    if is_prime(s[-1]):
        return 0
    
    dp = [[0] * (k + 1) for _ in range(n)]
    dp[0][1] = 1 if is_prime(s[0]) and not is_prime(s[0]) and minLength==1 else 0

    for i in range(1, n):
        for j in range(1, min(i + 2, k + 1)):
            for l in range(max(-1,i-minLength), -1, -1):
                if l == -1:
                    if j == 1 and is_prime(s[0]) and not is_prime(s[i]) and i + 1 >= minLength:
                        dp[i][j] = 1
                    else:
                        dp[i][j] = 0
                    break
                if is_prime(s[l + 1]) and not is_prime(s[i]):
                    dp[i][j] = (dp[i][j] + dp[l][j - 1]) % MOD
                else:
                    continue
        if is_prime(s[0]) and not is_prime(s[i]) and i + 1 >= minLength and k == 1:
            dp[i][1] = 1
    return dp[n - 1][k]
	```
			
