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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code