Optimal Partition of String (LeetCode 2405): Solution and Explanation
Split a lowercase string into the fewest substrings such that no substring contains the same letter twice, and return how many substrings that takes.
Intuition
A cut is only forced when the next letter already sits in the current substring. Until then, adding the letter costs nothing.
Cutting later is never worse than cutting earlier: a shorter current substring leaves more letters for the next one, which can only make it hit a repeat sooner.
So walk left to right, remember the letters of the open substring, and start a new one exactly when a letter repeats.
Approach: Greedy scan with a letter set
- Set
parts = 1and start with an empty set (or a 26-bit mask) of letters. - For each character
ch, check whether it is already in the set. - If it is, the current substring ends before
ch: add 1 topartsand clear the set. - Add
chto the set and keep going. - Return
parts.
Step-by-step example
s = "abacaba"
- Read a, then b: the open substring is "ab".
- Read a: it repeats, so cut. parts = 2, open substring "a".
- Read c: open substring "ac".
- Read a: it repeats, so cut. parts = 3, open substring "a".
- Read b: open substring "ab".
- Read a: it repeats, so cut. parts = 4, open substring "a".
- Return 4. The substrings are "ab", "ac", "ab", "a".
Code: Python, Java, C++
1class Solution:
2 def partitionString(self, s: str) -> int:
3 seen = set()
4 parts = 1
5 for ch in s:
6 if ch in seen:
7 parts += 1
8 seen.clear()
9 seen.add(ch)
10 return parts1class Solution {
2 public int partitionString(String s) {
3 int seen = 0;
4 int parts = 1;
5 for (char ch : s.toCharArray()) {
6 int bit = 1 << (ch - 'a');
7 if ((seen & bit) != 0) {
8 parts++;
9 seen = 0;
10 }
11 seen |= bit;
12 }
13 return parts;
14 }
15}1class Solution {
2public:
3 int partitionString(string s) {
4 int seen = 0;
5 int parts = 1;
6 for (char ch : s) {
7 int bit = 1 << (ch - 'a');
8 if (seen & bit) {
9 parts++;
10 seen = 0;
11 }
12 seen |= bit;
13 }
14 return parts;
15 }
16};Time and space complexity
O(n)
Each character is read once and the set check is constant time.
O(1)
The set never holds more than 26 letters, or a single integer when you use a bitmask.
Other approaches
Last-seen index array (time O(n), space O(1))
Store the last index of each letter and the start of the open substring. A letter repeats inside the substring when its last index is at least the start. This avoids clearing a set.
Dynamic programming over cut points (time O(26n), space O(n))
dp[i] = fewest substrings for the prefix of length i, trying every valid last substring. Correct but quadratic and unnecessary once you see the greedy argument.
Common mistakes
- Starting the count at 0 and forgetting the last open substring. A non-empty string always has at least one part.
- Clearing the set but forgetting to add the repeating letter to the new substring.
- Counting distinct letters overall instead of per substring.
Frequently asked questions
Why is cutting as late as possible optimal?
Compare any valid partition with the greedy one. The greedy's first substring ends at or after the other partition's first cut, so after each cut the greedy has covered at least as much of the string. By induction it never needs more substrings.
Why use a bitmask instead of a set?
There are only 26 lowercase letters, so one integer holds the whole set. Checking a bit and resetting to 0 are both single operations, which is faster than hashing.
How does this relate to Partition Labels?
Both scan once and decide cut points greedily, but Partition Labels wants each letter in only one part across the whole string, while here a letter may appear in many parts, just never twice in one.