Maximize partitions such that no two substrings have any common character
Last Updated :
13 Sep, 2022
Given string str of size N, the task is to print the number of substrings formed after maximum possible partitions such that no two substrings have a common character.
Examples:
Input : str = “ababcbacadefegdehijhklij”
Output : 3
Explanation:
Partitioning at the index 8 and at 15 produces three substrings “ababcbaca”, “defegde” and “hijhklij” such that none of them have a common character. So, the maximum partitions is 3.
Input: str = “aaa”
Output: 1
Explanation:
Since, the string consists of a single character, no partition can be performed.
Approach:
- Find the last index of every unique character from the end of the string and store it in a map.
- Traverse the array from the index 0 up to the last index and make a separate variable to store the ending index of partition.
- Traverse for every variable in the string and check if the end of the partition, denoted by the index of str[i] stored in the map, is greater than the previous end. If so, update it.
- Check if the current variable exceeds the end of the partition. It means the first partition is found. Update the end of the partition to max(k, mp[str[i]]).
- Traverse the whole string and use a similar process to find the next partition and so on.
Below is the implementation of the above approach :
C++
#include
using namespace std;
int maximum_partition(string str)
{
int i = 0, j = 0, k = 0;
int c = 0, r = 0;
unordered_map m;
for (i = str.length() - 1;
i >= 0;
i--) {
if (m[str[i]] == 0) {
m[str[i]] = i;
}
}
i = 0;
k = m[str[i]];
for (i = 0; i < str.length(); i++) {
if (i <= k) {
c = c + 1;
k = max(k, m[str[i]]);
}
else {
r = r + 1;
c = 1;
k = max(k, m[str[i]]);
}
}
if (c != 0) {
r = r + 1;
}
return r;
}
int main()
{
string str
= "ababcbacadefegdehijhklij" ;
cout << maximum_partition(str);
}
|
Java
import java.util.*;
class GFG{
static int maximum_partition(String str)
{
int i = 0 , j = 0 , k = 0 ;
int c = 0 , r = 0 ;
HashMap<Character,
Integer> m = new HashMap<>();
for (i = str.length() - 1 ;
i >= 0 ; i--)
{
if (!m.containsKey(str.charAt(i)))
{
m.put(str.charAt(i), i);
}
}
i = 0 ;
k = m.get(str.charAt(i));
for (i = 0 ; i < str.length(); i++)
{
if (i <= k)
{
c = c + 1 ;
k = Math.max(k, m.get(str.charAt(i)));
}
else
{
r = r + 1 ;
c = 1 ;
k = Math.max(k, m.get(str.charAt(i)));
}
}
if (c != 0 )
{
r = r + 1 ;
}
return r;
}
public static void main(String[] args)
{
String str = "ababcbacadefegdehijhklij" ;
System.out.print(maximum_partition(str));
}
}
|
Python 3
def maximum_partition(strr):
i = 0
j = 0
k = 0
c = 0
r = 0
m = {}
for i in range ( len (strr) - 1 , - 1 , - 1 ):
if (strr[i] not in m):
m[strr[i]] = i
i = 0
k = m[strr[i]]
for i in range ( len (strr)):
if (i < = k):
c = c + 1
k = max (k, m[strr[i]])
else :
r = r + 1
c = 1
k = max (k, m[strr[i]])
if (c ! = 0 ):
r = r + 1
return r
if __name__ = = '__main__' :
strr = "ababcbacadefegdehijhklij"
print (maximum_partition(strr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximum_partition(String str)
{
int i = 0, j = 0, k = 0;
int c = 0, r = 0;
Dictionary< char ,
int > m = new Dictionary< char ,
int >();
for (i = str.Length - 1;
i >= 0; i--)
{
if (!m.ContainsKey(str[i]))
{
m.Add(str[i], i);
}
}
i = 0;
k = m[str[i]];
for (i = 0; i < str.Length; i++)
{
if (i <= k)
{
c = c + 1;
k = Math.Max(k, m[str[i]]);
}
else
{
r = r + 1;
c = 1;
k = Math.Max(k, m[str[i]]);
}
}
if (c != 0)
{
r = r + 1;
}
return r;
}
public static void Main(String[] args)
{
String str = "ababcbacadefegdehijhklij" ;
Console.Write(maximum_partition(str));
}
}
|
Javascript
<script>
function maximum_partition( str)
{
var i = 0, j = 0, k = 0;
var c = 0, r = 0;
var m = new Map();
for (i = str.length - 1;
i >= 0;
i--) {
if (!m.has(str[i])) {
m.set(str[i],i);
}
}
i = 0;
k = m.get(str[i]);
for (i = 0; i < str.length; i++) {
if (i <= k) {
c = c + 1;
k = Math.max(k, m.get(str[i]));
}
else {
r = r + 1;
c = 1;
k = Math.max(k, m.get(str[i]));
}
}
if (c != 0) {
r = r + 1;
}
return r;
}
var str
= "ababcbacadefegdehijhklij" ;
document.write( maximum_partition(str));
</script>
|
Time complexity: O(NlogN)
Auxiliary Space: O(N) for map m
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...