πDay 5. Maximize partitions in a String π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a string s consisting of lowercase English alphabets, your task is to determine the maximum number of substrings that can be formed by partitioning s such that no two substrings share common characters.
Note: Sorry for uploading late, my exam is going on.
π Example Walkthrough:
Example 1:
Input:
s = "acbbcc"
Output:
2
Explanation:
Possible partitions:
"a"
and"cbbcc"
"ac"
and"bbcc"
The maximum possible substrings with no common characters are 2.
Example 2:
Input:
s = "ababcbacadefegdehijhklij"
Output:
3
Explanation:
Partitioning at index 8 and 15 produces three substrings:
"ababcbaca"
"defegde"
"hijhklij"
Each of these substrings contains unique characters within them.
Example 3:
Input:
s = "aaa"
Output:
1
Explanation:
Since the string consists of the same character, no partition can be performed. The entire string itself is a single valid substring.
Constraints:
$1 \leq |s| \leq 10^5$
$'a' \leq s[i] \leq 'z'$
π― My Approach:
Greedy Approach Using Last Occurrence
Find the last occurrence of each character and store it.
Traverse the string and maintain an interval that expands based on character occurrences.
When the current index reaches the last occurrence of the characters in that segment, count it as a partition.
Algorithm Steps:
Precompute the last occurrence of each character in O(N) time.
Traverse the string, maintaining an
end
pointer that marks the furthest last occurrence of encountered characters.If the current index reaches
end
, increment the partition count.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(N), as we scan the string twice (once to find last occurrences, once for partitioning).
Expected Auxiliary Space Complexity: O(1), since we use a fixed-size array (26 elements for lowercase letters).
π Solution Code
Code (C++)
class Solution {
public:
int maxPartitions(string &s) {
int ans = 0, end = 0;
vector<int> last(26);
for (int i = 0; i < s.size(); i++)
last[s[i] - 'a'] = i;
for (int i = 0; i < s.size(); i++) {
end = max(end, last[s[i] - 'a']);
if (i == end) ans++;
}
return ans;
}
};
Code (Java)
class Solution {
public int maxPartitions(String s) {
int[] last = new int[26];
for (int i = 0; i < s.length(); i++)
last[s.charAt(i) - 'a'] = i;
int count = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
end = Math.max(end, last[s.charAt(i) - 'a']);
if (i == end)
count++;
}
return count;
}
}
Code (Python)
class Solution:
def maxPartitions(self, s: str) -> int:
last = {c: i for i, c in enumerate(s)}
count = end = 0
for i, c in enumerate(s):
end = max(end, last[c])
if i == end:
count += 1
return count
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated