2478. Number of Beautiful Partitions LeetCode Solution
In this guide, you will get 2478. Number of Beautiful Partitions LeetCode Solution with the best time and space complexity. The solution to Number of Beautiful Partitions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Beautiful Partitions solution in C++
- Number of Beautiful Partitions solution in Java
- Number of Beautiful Partitions solution in Python
- Additional Resources

Problem Statement of Number of Beautiful Partitions
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'.
Complexity Analysis
- Time Complexity: O(nk)
- Space Complexity: O(nk)
2478. Number of Beautiful Partitions LeetCode Solution in C++
class Solution {
public:
int beautifulPartitions(string s, int k, int minLength) {
if (!isPrime(s.front()) || isPrime(s.back()))
return 0;
vector<vector<int>> mem(s.length(), vector<int>(k, -1));
return beautifulPartitions(s, minLength, k - 1, minLength, mem);
}
private:
static constexpr int kMod = 1'000'000'007;
// Returns the number of beautiful partitions of s[i..n) with k bars (|) left.
int beautifulPartitions(const string& s, int i, int k, int minLength,
vector<vector<int>>& mem) {
if (i <= s.length() && k == 0)
return 1;
if (i >= s.length())
return 0;
if (mem[i][k] != -1)
return mem[i][k];
// Don't split between s[i - 1] and s[i].
int res = beautifulPartitions(s, i + 1, k, minLength, mem) % kMod;
// Split between s[i - 1] and s[i].
if (isPrime(s[i]) && !isPrime(s[i - 1]))
res += beautifulPartitions(s, i + minLength, k - 1, minLength, mem);
return mem[i][k] = res % kMod;
}
bool isPrime(char c) {
return c == '2' || c == '3' || c == '5' || c == '7';
}
};
/* code provided by PROGIEZ */
2478. Number of Beautiful Partitions LeetCode Solution in Java
class Solution {
public int beautifulPartitions(String s, int k, int minLength) {
if (!isPrime(s.charAt(0)) || isPrime(s.charAt(s.length() - 1)))
return 0;
Integer[][] mem = new Integer[s.length()][k];
return beautifulPartitions(s, minLength, k - 1, minLength, mem);
}
private static final int kMod = 1_000_000_007;
// Returns the number of beautiful partitions of s[i..n) with k bars (|) left.
private int beautifulPartitions(final String s, int i, int k, int minLength, Integer[][] mem) {
if (i <= s.length() && k == 0)
return 1;
if (i >= s.length())
return 0;
if (mem[i][k] != null)
return mem[i][k];
// Don't split between s[i - 1] and s[i].
int ans = beautifulPartitions(s, i + 1, k, minLength, mem) % kMod;
// Split between s[i - 1] and s[i].
if (isPrime(s.charAt(i)) && !isPrime(s.charAt(i - 1)))
ans += beautifulPartitions(s, i + minLength, k - 1, minLength, mem);
return mem[i][k] = ans % kMod;
}
private boolean isPrime(char c) {
return c == '2' || c == '3' || c == '5' || c == '7';
}
}
// code provided by PROGIEZ
2478. Number of Beautiful Partitions LeetCode Solution in Python
class Solution:
def beautifulPartitions(self, s: str, k: int, minLength: int) -> int:
def isPrime(c: str) -> bool:
return c in '2357'
if not isPrime(s[0]) or isPrime(s[-1]):
return 0
kMod = 1_000_000_007
@lru_cache(None)
def dp(i: int, k: int) -> int:
"""
Returns the number of beautiful partitions of s[i..n) with k bars (|)
left.
"""
if i <= len(s) and k == 0:
return 1
if i >= len(s):
return 0
# Don't split between s[i - 1] and s[i].
ans = dp(i + 1, k) % kMod
# Split between s[i - 1] and s[i].
if isPrime(s[i]) and not isPrime(s[i - 1]):
ans += dp(i + minLength, k - 1)
return ans % kMod
return dp(minLength, k - 1)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.