Number of Beautiful Partitions - Problem
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:
sis partitioned intoknon-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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "23331", k = 2, minLength = 1
›
Output:
1
💡 Note:
Only one beautiful partition: ["233", "31"]. "233" starts with prime '2' and ends with non-prime '3'. "31" starts with prime '3' and ends with non-prime '1'.
Example 2 — No Valid Partition
$
Input:
s = "23", k = 3, minLength = 1
›
Output:
0
💡 Note:
Cannot partition string of length 2 into 3 parts, so answer is 0.
Example 3 — Multiple Ways
$
Input:
s = "3213", k = 2, minLength = 1
›
Output:
0
💡 Note:
No beautiful partitions exist. For any 2-part partition, we cannot satisfy both parts starting with prime and ending with non-prime digits. For example, ["321", "3"] has "321" valid (starts with '3', ends with '1'), but "3" is invalid (starts and ends with prime '3').
Constraints
- 1 ≤ k ≤ min(s.length, 100)
- 1 ≤ minLength ≤ s.length
- 1 ≤ s.length ≤ 1000
- s consists of digits '1' through '9'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code