Generate lexicographically smallest string of 0, 1 and 2 with adjacent swaps allowed
Last Updated :
21 Mar, 2023
Given a string str containing only characters 0, 1, and 2, you can swap any two adjacent (consecutive) characters 0 and 1 or any two adjacent (consecutive) characters 1 and 2. The task is to obtain the minimum possible (lexicographically) string by using these swaps an arbitrary number of times.
Examples:
Input: str = “100210”
Output: 001120
We can swap 0 and 1 OR we can swap 1 and 2. Swapping 0 and 2 is not allowed. All the swaps can happen for adjacent only.
Input: str = “2021”
Output: 1202
Note that 0 and 2 cannot be swapped
Approach: You can print all the 1s together as 1 can be swapped with either of the other characters while 0 and 2 can not be swapped, so all the 0s and 2s will follow the same order as the original string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printString(string str, int n)
{
int ones = 0;
for ( int i = 0; i < n; i++)
if (str[i] == '1' )
ones++;
bool used = false ;
for ( int i = 0; i < n; i++) {
if (str[i] == '2' && !used) {
used = 1;
for ( int j = 0; j < ones; j++)
cout << "1" ;
}
if (str[i] != '1' )
cout << str[i];
}
if (!used)
for ( int j = 0; j < ones; j++)
cout << "1" ;
}
int main()
{
string str = "100210" ;
int n = str.length();
printString(str, n);
return 0;
}
|
Java
class GFG
{
static void printString( char [] str, int n)
{
int ones = 0 ;
for ( int i = 0 ; i < n; i++)
if (str[i] == '1' )
ones++;
boolean used = false ;
for ( int i = 0 ; i < n; i++)
{
if (str[i] == '2' && !used)
{
used = true ;
for ( int j = 0 ; j < ones; j++)
System.out.print( "1" );
}
if (str[i] != '1' )
System.out.print(str[i]);
}
if (!used)
for ( int j = 0 ; j < ones; j++)
System.out.print( "1" );
}
public static void main(String[] args)
{
String str = "100210" ;
int n = str.length();
printString(str.toCharArray(), n);
}
}
|
Python3
def printString(Str1, n):
ones = 0
for i in range (n):
if (Str1[i] = = '1' ):
ones + = 1
used = False
for i in range (n):
if (Str1[i] = = '2' and used = = False ):
used = 1
for j in range (ones):
print ( "1" , end = "")
if (Str1[i] ! = '1' ):
print (Str1[i], end = "")
if (used = = False ):
for j in range (ones):
print ( "1" , end = "")
Str1 = "100210"
n = len (Str1)
printString(Str1, n)
|
C#
using System;
class GFG
{
static void printString( char [] str, int n)
{
int ones = 0;
for ( int i = 0; i < n; i++)
if (str[i] == '1' )
ones++;
bool used = false ;
for ( int i = 0; i < n; i++)
{
if (str[i] == '2' && !used)
{
used = true ;
for ( int j = 0; j < ones; j++)
Console.Write( "1" );
}
if (str[i] != '1' )
Console.Write(str[i]);
}
if (!used)
for ( int j = 0; j < ones; j++)
Console.Write( "1" );
}
public static void Main(String[] args)
{
String str = "100210" ;
int n = str.Length;
printString(str.ToCharArray(), n);
}
}
|
PHP
<?php
function printString( $str , $n )
{
$ones = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $str [ $i ] == '1' )
$ones ++;
$used = false;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] == '2' && ! $used )
{
$used = 1;
for ( $j = 0; $j < $ones ; $j ++)
echo "1" ;
}
if ( $str [ $i ] != '1' )
echo $str [ $i ];
}
if (! $used )
for ( $j = 0; $j < $ones ; $j ++)
echo "1" ;
}
$str = "100210" ;
$n = strlen ( $str );
printString( $str , $n );
?>
|
Javascript
<script>
function printString(str, n)
{
let ones = 0;
for (let i = 0; i < n; i++)
if (str[i] == '1' )
ones++;
let used = false ;
for (let i = 0; i < n; i++)
{
if (str[i] == '2' && !used)
{
used = true ;
for (let j = 0; j < ones; j++)
document.write( "1" );
}
if (str[i] != '1' )
document.write(str[i]);
}
if (!used)
for (let j = 0; j < ones; j++)
document.write( "1" );
}
let str = "100210" ;
let n = str.length;
printString(str.split( '' ), n);
</script>
|
Time Complexity: O(n2), // since two nested loops are used the time taken by the algorithm to complete all operation is quadratic.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...