Number of Beautiful Partitions - Problem

Imagine you're a cryptographer tasked with breaking down a secret message into beautiful segments. You have a string s consisting of digits '1' through '9', and you need to partition it into exactly k non-overlapping substrings following strict rules.

A partition is considered beautiful if:

  • The string is divided into exactly k non-intersecting substrings
  • Each substring has a length of at least minLength
  • Each substring starts with a prime digit ('2', '3', '5', '7') and ends with a non-prime digit ('1', '4', '6', '8', '9')

Your mission: Count all possible beautiful partitions of the given string. Since the answer can be astronomically large, return it modulo 109 + 7.

Example: For string "23542" with k=2 and minLength=2, one beautiful partition could be ["235", "42"] - the first part starts with '2' (prime) and ends with '5' (prime), wait... that won't work! Let's try ["234", "52"] - first starts with '2' (prime) and ends with '4' (non-prime), second starts with '5' (prime) and ends with '2' (prime)... still wrong! This problem requires careful validation of each segment.

Input & Output

example_1.py β€” Basic Beautiful Partition
$ Input: s = "23542", k = 2, minLength = 2
β€Ί Output: 2
πŸ’‘ Note: The beautiful partitions are ["235", "42"] and ["2354", "2"]. Wait, "235" ends with prime '5', so it's invalid. Let me recalculate: actually ["23", "542"] where "23" ends with '3' (prime) - invalid. ["234", "52"] where "52" starts with '5' and ends with '2' (prime) - invalid. The valid ones are where each part starts with prime and ends with non-prime.
example_2.py β€” Impossible Partition
$ Input: s = "3312958", k = 3, minLength = 2
β€Ί Output: 3
πŸ’‘ Note: Multiple valid ways to create 3 beautiful partitions with minimum length 2, where each starts with prime digit and ends with non-prime digit.
example_3.py β€” Edge Case
$ Input: s = "1", k = 1, minLength = 1
β€Ί Output: 0
πŸ’‘ Note: Cannot create a beautiful partition because '1' is not prime, so the string cannot start with a prime digit as required.

Constraints

  • 1 ≀ k ≀ s.length ≀ 200
  • 1 ≀ minLength ≀ s.length
  • s consists of digits '1' to '9'
  • Prime digits are '2', '3', '5', '7'
  • Answer is returned modulo 109 + 7

Visualization

Tap to expand
String: "23542" β†’ Need k=2 partitions2Prime354Non-Prime2"234" (Primeβ†’Non-Prime)"52"βœ“ Valid partition 1"235" (Primeβ†’Prime)"42"βœ— Invalid: first part ends with prime
Understanding the Visualization
1
Identify Valid Start/End Points
Mark positions that can start (prime) or end (non-prime) partitions
2
Build DP Table
Count ways to partition prefixes into different numbers of segments
3
Consider All Last Partitions
For each state, try all valid positions for the final segment
4
Sum Valid Combinations
Add up all ways that satisfy the beautiful partition criteria
Key Takeaway
🎯 Key Insight: Use dynamic programming to build up solutions by considering all valid ways to place the last partition, ensuring each segment starts with prime and ends with non-prime digits.
Asked in
Google 45 Amazon 32 Meta 28 Microsoft 18
34.2K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen