Minimum Substring Partition of Equal Character Frequency - Problem

Given a string s, you need to partition it into one or more balanced substrings.

For example, if s == "ababcc" then ("abab", "c", "c"), ("ab", "abc", "c"), and ("ababcc") are all valid partitions, but ("a", "bab", "cc"), ("aba", "bc", "c"), and ("ab", "abcc") are not valid because they contain unbalanced substrings.

Return the minimum number of substrings that you can partition s into.

Note: A balanced string is a string where each character in the string occurs the same number of times.

Input & Output

Example 1 — Basic Case
$ Input: s = "ababcc"
Output: 3
💡 Note: We can partition into ("abab", "c", "c"). "abab" has a:2, b:2 (balanced), each "c" has c:1 (balanced). Total: 3 partitions.
Example 2 — Single Character
$ Input: s = "aaaa"
Output: 1
💡 Note: The entire string "aaaa" has a:4, so it's balanced. We need only 1 partition.
Example 3 — All Different Characters
$ Input: s = "abc"
Output: 3
💡 Note: Each character appears once, so "a", "b", "c" are each balanced. We need 3 partitions.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of lowercase English letters

Visualization

Tap to expand
Minimum Substring Partition Problema b a b c cInput: "ababcc""abab"a:2, b:2 ✓"c"c:1 ✓"c"c:1 ✓Output: 3 (minimum partitions)
Understanding the Visualization
1
Input
String "ababcc" needs partitioning
2
Process
Find cuts where each part has equal character frequencies
3
Output
Minimum 3 partitions: "abab", "c", "c"
Key Takeaway
🎯 Key Insight: Use dynamic programming to try all possible partition points and find the minimum number of balanced substrings
Asked in
Google 25 Amazon 18 Microsoft 15
23.4K Views
Medium Frequency
~25 min Avg. Time
892 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