Lengths of maximized partitions of a string such that each character of the string appears in one substring
Given string str of lowercase alphabets, split the given string into as many substrings as possible such that each character from the given string appears in a single substring. The task is to print the length of all such partitions.
Examples:
Input: str = “acbbcc”
Output: 1 5
Explanation:
Possible partitions where each character of the strings occurs in at most one partition are “a” and “cbbcc”.
Therefore, the length is {1, 5}
Input: str = “abaccbdeffed”
Output: 6 6
Explanation:
Possible partitions where each character of the strings occurs in at most one partition are “abaccb” and “deffed”.
Therefore, the length is {6, 6}
Approach: This problem can be solved easily by using the Greedy Approach. Follow the steps given below to solve the problem.
- Store the last index of all characters in the string.
- Since the string contains only lowercase letters, simply use an array of fixed size 26 to store the last indices of each character.
- Iterate over the given string and follow the steps below:
- Add the current character to the partition if the last position of the character exceeds the current index and increase the length of the partition.
- If the last index of the current character is equal to the current index, then store its length and proceed to the next character and so on.
- After completing the above steps, print all the lengths stored.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void partitionString(string s)
{
int n = s.size();
vector< int > ans;
if (n == 0) {
cout << "-1" ;
return ;
}
vector< int > last_pos(26, -1);
for ( int i = n - 1; i >= 0; --i) {
if (last_pos[s[i] - 'a' ] == -1) {
last_pos[s[i] - 'a' ] = i;
}
}
int minp = -1, plen = 0;
for ( int i = 0; i < n; ++i) {
int lp = last_pos[s[i] - 'a' ];
minp = max(minp, lp);
++plen;
if (i == minp) {
ans.push_back(plen);
minp = -1;
plen = 0;
}
}
for ( int i = 0;
i < ( int )ans.size(); i++) {
cout << ans[i] << " " ;
}
}
int main()
{
string str = "acbbcc" ;
partitionString(str);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void partitionString(String s)
{
int n = s.length();
Vector<Integer> ans =
new Vector<Integer>();
if (n == 0 )
{
System.out.print( "-1" );
return ;
}
int []last_pos = new int [ 26 ];
Arrays.fill(last_pos, - 1 );
for ( int i = n - 1 ; i >= 0 ; --i)
{
if (last_pos[s.charAt(i) - 'a' ] == - 1 )
{
last_pos[s.charAt(i) - 'a' ] = i;
}
}
int minp = - 1 , plen = 0 ;
for ( int i = 0 ; i < n; ++i)
{
int lp = last_pos[s.charAt(i) - 'a' ];
minp = Math.max(minp, lp);
++plen;
if (i == minp)
{
ans.add(plen);
minp = - 1 ;
plen = 0 ;
}
}
for ( int i = 0 ; i < ( int )ans.size(); i++)
{
System.out.print(ans.get(i) + " " );
}
}
public static void main(String[] args)
{
String str = "acbbcc" ;
partitionString(str);
}
}
|
Python3
def partitionString(s):
n = len (s)
ans = []
if (n = = 0 ):
print ( "-1" )
return
last_pos = [ - 1 ] * 26
for i in range (n - 1 , - 1 , - 1 ):
if (last_pos[ ord (s[i]) - ord ( 'a' )] = = - 1 ):
last_pos[ ord (s[i]) - ord ( 'a' )] = i
minp = - 1
plen = 0
for i in range (n):
lp = last_pos[ ord (s[i]) - ord ( 'a' )]
minp = max (minp, lp)
plen + = 1
if (i = = minp):
ans.append(plen)
minp = - 1
plen = 0
for i in range ( len (ans)):
print (ans[i], end = " " )
str = "acbbcc"
partitionString( str )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void partitionString(String s)
{
int n = s.Length;
List< int > ans = new List< int >();
if (n == 0)
{
Console.Write( "-1" );
return ;
}
int []last_pos = new int [26];
for ( int i = 0; i < 26; ++i)
{
last_pos[i] = -1;
}
for ( int i = n - 1; i >= 0; --i)
{
if (last_pos[s[i] - 'a' ] == -1)
{
last_pos[s[i] - 'a' ] = i;
}
}
int minp = -1, plen = 0;
for ( int i = 0; i < n; ++i)
{
int lp = last_pos[s[i] - 'a' ];
minp = Math.Max(minp, lp);
++plen;
if (i == minp)
{
ans.Add(plen);
minp = -1;
plen = 0;
}
}
for ( int i = 0; i < ( int )ans.Count; i++)
{
Console.Write(ans[i] + " " );
}
}
public static void Main(String[] args)
{
String str = "acbbcc" ;
partitionString(str);
}
}
|
Javascript
<script>
function partitionString(s)
{
let n = s.length;
let ans = [];
if (n == 0)
{
document.write( "-1" );
return ;
}
let last_pos = Array(26).fill(-1);
for (let i = n - 1; i >= 0; --i)
{
if (last_pos[s[i].charCodeAt() - 'a' .charCodeAt()] == -1)
{
last_pos[s[i].charCodeAt() - 'a' .charCodeAt()] = i;
}
}
let minp = -1, plen = 0;
for (let i = 0; i < n; ++i)
{
let lp = last_pos[s[i].charCodeAt() - 'a' .charCodeAt()];
minp = Math.max(minp, lp);
++plen;
if (i == minp)
{
ans.push(plen);
minp = -1;
plen = 0;
}
}
for (let i = 0; i < ans.length; i++)
{
document.write(ans[i] + " " );
}
}
let str = "acbbcc" ;
partitionString(str);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
26 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...