Rearrange a string S1 such that another given string S2 is not its subsequence
Last Updated :
22 Nov, 2022
Given two strings S1 and S2 of size N and M respectively, the task is to rearrange characters in string S1 such that S2 is not a subsequence of it. If it is not possible to make such rearrangements, then print “-1”. Otherwise, print the rearranged string S1.
Examples:
Input: S1 = “abcd”, S2 = “ab”
Output: “dcba”
Explanation:
Rearrange the string S1 as “dcba”.
Now, string S2 = “ab” is not a subsequence of S1.
Input: S1 = “aa”, S2 = “a”
Output: -1
Approach: Follow the steps below to solve the problem:
- Store the frequency of characters in string S2 in an auxiliary array, say cnt[26].
- Initialize a variable, say ch, to store the unique characters present in string S2.
- Traverse the array cnt[] using the variable i. If the value of cnt[i] is equal to M, this means that S2 consists of only 1 unique character. Store this character in ch and break out of the loop.
- If the value of ch is not defined, then do the following:
- Otherwise, store the occurrence of ch in string S1 in freq. If the value of freq is less than M, print S1. Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rearrangeString(string s1, string s2)
{
int cnt[26] = { 0 };
for ( int i = 0; i < s2.size(); i++)
cnt[s2[i] - 'a' ]++;
int unique = 0;
for ( int i = 0; i < 26; i++)
if (cnt[i] != 0)
unique++;
if (unique == 1) {
int count_in_s2 = s2.size();
int count_in_s1 = 0;
for ( int i = 0; i < s1.size(); i++)
if (s1[i] == s2[0])
count_in_s1++;
if (count_in_s1 < count_in_s2) {
cout << s1;
return ;
}
cout << -1;
}
else {
int inc = 1;
for ( int i = 0; i < s2.size() - 1; i++)
if (s2[i] > s2[i + 1])
inc = 0;
if (inc == 1) {
sort(s1.begin(),
s1.end(),
greater< char >());
cout << s1;
}
else {
sort(s1.begin(), s1.end());
cout << s1;
}
}
}
int main()
{
string s1 = "abcd" , s2 = "ab" ;
rearrangeString(s1, s2);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void rearrangeString( char [] s1, char [] s2)
{
int [] cnt = new int [ 26 ];
for ( int i = 0 ; i < s2.length; i++)
cnt[s2[i] - 'a' ]++;
int unique = 0 ;
for ( int i = 0 ; i < 26 ; i++)
if (cnt[i] != 0 )
unique++;
if (unique == 1 )
{
int count_in_s2 = s2.length;
int count_in_s1 = 0 ;
for ( int i = 0 ; i < s1.length; i++)
if (s1[i] == s2[ 0 ])
count_in_s1++;
if (count_in_s1 < count_in_s2)
{
System.out.print( new String(s1));
return ;
}
System.out.print(- 1 );
}
else
{
int inc = 1 ;
for ( int i = 0 ; i < s2.length - 1 ; i++)
if (s2[i] > s2[i + 1 ])
inc = 0 ;
if (inc == 1 )
{
Arrays.sort(s1);
for ( int k = 0 ; k < s1.length / 2 ; k++)
{
char temp = s1[k];
s1[k] = s1[s1.length - k - 1 ];
s1[s1.length - k - 1 ] = temp;
}
System.out.print( new String(s1));
}
else
{
Arrays.sort(s1);
System.out.print( new String(s1));
}
}
}
public static void main(String[] args)
{
char [] s1 = "abcd" .toCharArray();
char [] s2 = "ab" .toCharArray();
rearrangeString(s1, s2);
}
}
|
Python3
def rearrangeString(s1, s2):
cnt = [ 0 ] * 26
for i in range ( len (s2)):
cnt[ ord (s2[i]) - ord ( 'a' )] + = 1
unique = 0
for i in range ( 26 ):
if (cnt[i] ! = 0 ):
unique + = 1
if (unique = = 1 ):
count_in_s2 = len (s2)
count_in_s1 = 0
for i in range ( len (s1)):
if (s1[i] = = s2[ 0 ]):
count_in_s1 + = 1
if (count_in_s1 < count_in_s2):
print (s1, end = "")
return
print ( - 1 , end = "")
else :
inc = 1
for i in range ( len (s2) - 1 ):
if (s2[i] > s2[i + 1 ]):
inc = 0
if (inc = = 1 ):
s1 = sorted (s1)[:: - 1 ]
print (" ".join(s1), end = " ")
else :
s1 = sorted (s1)
print (" ".join(s1), end = " ")
if __name__ = = '__main__' :
s1, s2 = "abcd" , "ab"
rearrangeString(s1, s2)
|
C#
using System;
class GFG
{
static void rearrangeString( char [] s1, char [] s2)
{
int [] cnt = new int [26];
for ( int i = 0; i < s2.Length; i++)
cnt[s2[i] - 'a' ]++;
int unique = 0;
for ( int i = 0; i < 26; i++)
if (cnt[i] != 0)
unique++;
if (unique == 1) {
int count_in_s2 = s2.Length;
int count_in_s1 = 0;
for ( int i = 0; i < s1.Length; i++)
if (s1[i] == s2[0])
count_in_s1++;
if (count_in_s1 < count_in_s2) {
Console.Write( new string (s1));
return ;
}
Console.Write(-1);
}
else {
int inc = 1;
for ( int i = 0; i < s2.Length - 1; i++)
if (s2[i] > s2[i + 1])
inc = 0;
if (inc == 1) {
Array.Sort(s1);
Array.Reverse(s1);
Console.Write( new string (s1));
}
else {
Array.Sort(s1);
Console.Write( new string (s1));
}
}
}
static void Main() {
char [] s1 = "abcd" .ToCharArray();
char [] s2 = "ab" .ToCharArray();
rearrangeString(s1, s2);
}
}
|
Javascript
<script>
function rearrangeString(s1, s2)
{
let cnt = new Array(26);
cnt.fill(0);
for (let i = 0; i < s2.length; i++)
cnt[s2[i].charCodeAt() -
'a' .charCodeAt()]++;
let unique = 0;
for (let i = 0; i < 26; i++)
if (cnt[i] != 0)
unique++;
if (unique == 1) {
let count_in_s2 = s2.length;
let count_in_s1 = 0;
for (let i = 0; i < s1.length; i++)
if (s1[i] == s2[0])
count_in_s1++;
if (count_in_s1 < count_in_s2) {
document.write(s1.join( "" ));
return ;
}
document.write(-1);
}
else {
let inc = 1;
for (let i = 0; i < s2.length - 1; i++)
if (s2[i].charCodeAt() >
s2[i + 1].charCodeAt())
inc = 0;
if (inc == 1) {
s1.sort();
s1.reverse();
document.write(s1.join( "" ));
}
else {
s1.sort();
document.write(s1.join( "" ));
}
}
}
let s1 = "abcd" .split( '' );
let s2 = "ab" .split( '' );
rearrangeString(s1, s2);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...