Maximized partitions of a string such that each character of the string appears in one substring
Given a string S, split the given string into as many substrings as possible such that each character from the given string appears in a single substring and print all these possible parts. The task is to print those substrings.
Examples:
Input: S = “ababcbacadefegdehijhklij”
Output:
ababcbaca defegde hijhklij
Explanation:
a, b, c are only present in the first string.
d, e, f, g are only present in the second string.
h, i, j, k, l are only present in the third string.
Input: S = “acbbcc”
Output:
a cbbcc
Explanation:
a are only present in the first string.
b, c are only present in the second string.
Approach: Follow the steps below to solve the problem:
- Store the last index of occurrence 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.
- Initialize an empty string ans = “” and iterate over the given string and follow the steps below:
- Add the current character to the string ans if the last position of the character is more than the current index and increase the length of the partition.
- If the last position of the current character is equal to the current index, then print the current string stored in ans and initialize ans to “” for storing the next partition of the string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void print_substring(string s)
{
int n = s.size();
string str = "" ;
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;
for ( int i = 0; i < n; ++i) {
int lp = last_pos[s[i] - 'a' ];
minp = max(minp, lp);
if (i == minp) {
str += s[i];
cout << str << ' ' ;
minp = -1;
str = "" ;
}
else {
str += s[i];
}
}
}
int main()
{
string S = "ababcbacadefegdehijhklij" ;
print_substring(S);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void print_substring(String s)
{
int n = s.length();
String str = "" ;
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 ;
for ( int i = 0 ; i < n; ++i) {
int lp = last_pos[s.charAt(i) - 'a' ];
minp = Math.max(minp, lp);
if (i == minp) {
str += s.charAt(i);
System.out.print(str + ' ' );
minp = - 1 ;
str = "" ;
}
else {
str += s.charAt(i);
}
}
}
public static void main(String[] args)
{
String S = "ababcbacadefegdehijhklij" ;
print_substring(S);
}
}
|
Python3
def print_substring(s):
n = len (s)
str = ""
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
for i in range (n):
lp = last_pos[ ord (s[i]) - ord ( 'a' )]
minp = max (minp, lp)
if (i = = minp):
str + = s[i]
print ( str , end = " " )
minp = - 1
str = ""
else :
str + = s[i]
S = "ababcbacadefegdehijhklij"
print_substring(S)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void print_substring(String s)
{
int n = s.Length;
String str = "" ;
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;
for ( int i = 0; i < n; ++i)
{
int lp = last_pos[s[i] - 'a' ];
minp = Math.Max(minp, lp);
if (i == minp)
{
str += s[i];
Console.Write(str + ' ' );
minp = -1;
str = "" ;
}
else
{
str += s[i];
}
}
}
public static void Main(String[] args)
{
String S = "ababcbacadefegdehijhklij" ;
print_substring(S);
}
}
|
Javascript
<script>
function print_substring(s)
{
let n = s.length;
let str = "" ;
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;
for (let i = 0; i < n; ++i) {
let lp = last_pos[s[i].charCodeAt() - 'a' .charCodeAt()];
minp = Math.max(minp, lp);
if (i == minp) {
str += s[i];
document.write(str + ' ');
minp = -1;
str = "" ;
}
else {
str += s[i];
}
}
}
let S = "ababcbacadefegdehijhklij" ;
print_substring(S);
</script>
|
Output
ababcbaca defegde hijhklij
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
06 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...