Given a string S consisting of N digits, the task is to partition the string into at most two increasing subsequences such that concatenation of them also forms an increasing string. If it is not possible to do so, then print “-1”.
Examples:
Input: S = “040425524644”
Output: 0022444 44556
Explanation:
One of the possible way to partition the given string S is {“0022444”, “44556”}. Both are in increasing order and the concatenation of them are also increasing “0022444” + ‘”4556″ = “002244444556”.
Therefore, print both the subsequence formed.
Input: S = “123456789”
Output: 123456789
Naive Approach: The simplest approach to solve the problem is to generate all possible subsequences and check whether any two non-overlapping subsequences satisfy the given condition or not. If found to be true, then print both the two subsequences.
Time Complexity: O(N*2N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by putting all the values less than X in the first subsequence and all the elements greater than X in the second subsequence and all the elements equal to X is decided on the basis of their position for all X in the range [0, 9]. Follow the steps below to solve the problem:
- Initialize a variable say, pos to store the position where less than pos are in the first subsequence, greater than pos in the second subsequence, and element equal to pos will be on the subsequence based on their position.
- Initialize an array res[] that will store which element belongs to which subsequence.
- Iterate pos in the range [0, 9] and perform the following steps:
- Initialize two variables last1 as 0 and last2 as pos which stores the last elements that have been put in subsequence 1 and 2 respectively.
- Initialize a boolean variable flag as 1 that stores whether the processed subsequence is valid or not.
- Iterate in the range [0, N-1] using i as a variable and perform the following steps:
- If last2 ≤ S[i], then modify the value of last2 as S[i] and res[i] as 2.
- Otherwise if last1 ≤ S[i], then modify the value of last1 as S[i] and res[i] as 1.
- Otherwise, modify the value of the flag as 0.
- Check if the value of last is greater than pos, then modify the value of flag as 0.
- If the value of flag is 1, then print the array res as the answer and break out of the loop.
- After completing the above steps, print -1 if no possible subsequence has been found.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSubsequence(string str)
{
int n = str.size();
char res[n];
for ( int i = 0; i < n; i++)
res[i] = 0;
for ( int pos = 0; pos <= 9; pos++) {
char lst1 = '0' ;
bool flag = 1;
char lst2 = pos + '0' ;
for ( int i = 0; i < n; i++) {
if (lst2 <= str[i]) {
res[i] = '2' ;
lst2 = str[i];
}
else if (lst1 <= str[i]) {
res[i] = '1' ;
lst1 = str[i];
}
else
flag = 0;
}
if (lst1 > pos + '0' )
flag = 0;
if (flag) {
string S1 = "" ;
string S2 = "" ;
for ( int i = 0; i < n; i++) {
if (res[i] == '1' ) {
S1 += str[i];
}
else {
S2 += str[i];
}
}
cout << S1 << ' ' << S2 << endl;
return ;
}
}
cout << "-1" ;
}
int main()
{
string S = "040425524644" ;
findSubsequence(S);
S = "123456789" ;
findSubsequence(S);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static void findSubsequence(String str)
{
int n = str.length();
char []res = new char [n];
for ( int i = 0 ; i < n; i++)
res[i] = 0 ;
for ( int pos = 0 ; pos <= 9 ; pos++) {
char lst1 = '0' ;
boolean flag = true ;
char lst2 = ( char ) (pos + '0' );
for ( int i = 0 ; i < n; i++) {
if (lst2 <= str.charAt(i)) {
res[i] = '2' ;
lst2 = str.charAt(i);
}
else if (lst1 <= str.charAt(i)) {
res[i] = '1' ;
lst1 = str.charAt(i);
}
else
flag = false ;
}
if (lst1 > pos + '0' )
flag = false ;
if (flag) {
String S1 = "" ;
String S2 = "" ;
for ( int i = 0 ; i < n; i++) {
if (res[i] == '1' ) {
S1 += str.charAt(i);
}
else {
S2 += str.charAt(i);
}
}
System.out.print(S1 + " " + S2 + "\n" );
return ;
}
}
System.out.print( "-1" );
}
public static void main(String[] args)
{
String S = "040425524644" ;
findSubsequence(S);
S = "123456789" ;
findSubsequence(S);
}
}
|
Python3
def findSubsequence( str ):
n = len ( str )
res = [ '0' for i in range (n)]
for pos in range ( 10 ):
lst1 = '0'
flag = 1
lst2 = chr (pos + 48 )
for i in range (n):
if (lst2 < = str [i]):
res[i] = '2'
lst2 = str [i]
elif (lst1 < = str [i]):
res[i] = '1'
lst1 = str [i]
else :
flag = 0
if (lst1 > chr (pos + 48 )):
flag = 0
if (flag):
S1 = ""
S2 = ""
for i in range (n):
if (res[i] = = '1' ):
S1 + = str [i]
else :
S2 + = str [i]
print (S1,S2)
return
print ( "-1" )
if __name__ = = '__main__' :
S = "040425524644"
findSubsequence(S)
S = "123456789"
findSubsequence(S)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findSubsequence( string str)
{
int n = str.Length;
char [] res = new char [n];
for ( int i = 0; i < n; i++)
res[i] = '0' ;
for ( int pos = 0; pos <= 9; pos++) {
char lst1 = '0' ;
bool flag = true ;
char lst2 = ( char ) (pos + '0' );
for ( int i = 0; i < n; i++) {
if (lst2 <= str[i]) {
res[i] = '2' ;
lst2 = str[i];
}
else if (lst1 <= str[i]) {
res[i] = '1' ;
lst1 = str[i];
}
else
flag = false ;
}
if (lst1 > pos + '0' )
flag = false ;
if (flag) {
string S1 = "" ;
string S2 = "" ;
for ( int i = 0; i < n; i++) {
if (res[i] == '1' ) {
S1 += str[i];
}
else {
S2 += str[i];
}
}
Console.WriteLine(S1 + ' ' + S2);
return ;
}
}
Console.Write( "-1" );
}
public static void Main()
{
string S = "040425524644" ;
findSubsequence(S);
S = "123456789" ;
findSubsequence(S);
}
}
|
Javascript
<script>
function findSubsequence(str) {
let n = str.length;
let res = new Array(n);
for (let i = 0; i < n; i++)
res[i] = 0;
for (let pos = 0; pos <= 9; pos++) {
let lst1 = '0' ;
let flag = 1;
let lst2 = String.fromCharCode(pos + '0' .charCodeAt(0));
for (let i = 0; i < n; i++) {
if (lst2.charCodeAt(0) <= str[i].charCodeAt(0)) {
res[i] = '2' ;
lst2 = str[i];
}
else if (lst1.charCodeAt(0) <= str[i].charCodeAt(0)) {
res[i] = '1' ;
lst1 = str[i];
}
else
flag = 0;
}
if (lst1.charCodeAt(0) > pos + '0' .charCodeAt(0)) {
flag = 0;
}
if (flag) {
let S1 = "" ;
let S2 = "" ;
for (let i = 0; i < n; i++) {
if (res[i] == '1' ) {
S1 += str[i];
}
else {
S2 += str[i];
}
}
document.write(S1 + ' ' + S2 + '<br>' );
return ;
}
}
document.write( "-1" );
}
let S = "040425524644" ;
findSubsequence(S);
S = "123456789" ;
findSubsequence(S);
</script>
|
Output:
0022444 44556
123456789
Time Complexity: O(N^2)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Mar, 2023
Like Article
Save Article