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:
2
💡 Note:
Two beautiful partitions: ["321", "3"] and ["32", "13"]. Both satisfy the prime start and non-prime end constraints.
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
Understanding the Visualization
1
Input
String "23331" with k=2, minLength=1
2
Constraints
Each part starts with prime (2,3,5,7) and ends with non-prime (1,4,6,8,9)
3
Output
Count of valid partitions: 1
Key Takeaway
🎯 Key Insight: Use DP to count valid ways, checking prime/non-prime constraints for each substring
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code