Minimize given Number by swapping adjacent digits with odd difference
Given a string S of length N representing an integer that consists of only characters ‘1’, ‘2’ and ‘3’, the task is to find the smallest number that can be formed from the string by swapping the adjacent characters if their absolute difference is odd any number of times.
Examples:
Input: S = “213123”
Output: 122313
Explanation: The integer representing the output string (i.e. 122313) is smaller than the input string.
The swapping done are as follows (characters to be swapped are in bold) :
213123 -> 123123
123123 -> 123213
123213 -> 122313
Input: S = “122333”
Output: 122333
Approach: This problem can be easily solved by using the greedy approach using the following observation:
1 and 3 cannot be swapped as their difference is 2 (which is even).
1 and 2 can be swapped and so can be 2 and 3 as their difference is 1 (odd).
So keeping that in mind all the 2s can be placed at any position in the resultant string. The optimal position is to place all the 2s after the 1s which are situated at the starting of the string.
Follow the steps mentioned below to implement the observation:
- Count all the 2s present in the string.
- Keep the group of 1s unchanged if they are at the starting of the string i.e. keep them in the starting in the resultant string also.
- Add all the 2s of the string after this group of 1s.
- Finally, add the remaining occurrences of 1 and 3 into the resultant string in the same order as they were in given string (since 1 and 3 cannot be swapped).
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string findMinimumString(string S)
{
int N = S.length();
int pos = -1;
int count1 = 0, count2 = 0;
for ( int i = 0; i < N; i++) {
if (pos == -1 && S[i] == '1' )
count1++;
else if (pos == -1 && S[i] == '3' )
pos = i;
else if (S[i] == '2' )
count2++;
}
string answer = "" ;
while (count1--)
answer += '1' ;
while (count2--)
answer += '2' ;
if (pos != -1) {
while (pos < N) {
if (S[pos] != '2' )
answer += S[pos];
pos++;
}
}
return answer;
}
int main()
{
string S = "213123" ;
string minimum_string
= findMinimumString(S);
cout << minimum_string;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static String findMinimumString(String S)
{
int N = S.length();
int pos = - 1 ;
int count1 = 0 , count2 = 0 ;
for ( int i = 0 ; i < N; i++) {
if (pos == - 1 && S.charAt(i) == '1' )
count1++;
else if (pos == - 1 && S.charAt(i) == '3' )
pos = i;
else if (S.charAt(i) == '2' )
count2++;
}
String answer = "" ;
while ((count1--) != 0 )
answer += '1' ;
while ((count2--) != 0 )
answer += '2' ;
if (pos != - 1 ) {
while (pos < N) {
if (S.charAt(pos) != '2' )
answer += S.charAt(pos);
pos++;
}
}
return answer;
}
public static void main(String[] args)
{
String S = "213123" ;
String minimum_string = findMinimumString(S);
System.out.print(minimum_string);
}
}
|
Python3
def findMinimumString(S) :
N = len (S)
pos = - 1
count1 = 0
count2 = 0
for i in range (N) :
if (pos = = - 1 and S[i] = = '1' ):
count1 + = 1
elif (pos = = - 1 and S[i] = = '3' ):
pos = i
elif (S[i] = = '2' ) :
count2 + = 1
answer = ""
while (count1) :
answer + = '1'
count1 - = 1
while (count2):
answer + = '2'
count2 - = 1
if (pos ! = - 1 ) :
while (pos < N) :
if (S[pos] ! = '2' ):
answer + = S[pos]
pos + = 1
return answer
S = "213123"
minimum_string = findMinimumString(S)
print (minimum_string)
|
C#
using System;
class GFG
{
static String findMinimumString( string S)
{
int N = S.Length;
int pos = -1;
int count1 = 0, count2 = 0;
for ( int i = 0; i < N; i++) {
if (pos == -1 && S[i] == '1' )
count1++;
else if (pos == -1 && S[i] == '3' )
pos = i;
else if (S[i] == '2' )
count2++;
}
string answer = "" ;
while ((count1--) != 0)
answer += '1' ;
while ((count2--) != 0)
answer += '2' ;
if (pos != -1) {
while (pos < N) {
if (S[pos] != '2' )
answer += S[pos];
pos++;
}
}
return answer;
}
public static void Main()
{
string S = "213123" ;
string minimum_string = findMinimumString(S);
Console.Write(minimum_string);
}
}
|
Javascript
<script>
const findMinimumString = (S) => {
let N = S.length;
let pos = -1;
let count1 = 0, count2 = 0;
for (let i = 0; i < N; i++) {
if (pos == -1 && S[i] == '1' )
count1++;
else if (pos == -1 && S[i] == '3' )
pos = i;
else if (S[i] == '2' )
count2++;
}
let answer = "" ;
while (count1--)
answer += '1' ;
while (count2--)
answer += '2' ;
if (pos != -1) {
while (pos < N) {
if (S[pos] != '2' )
answer += S[pos];
pos++;
}
}
return answer;
}
let S = "213123" ;
let minimum_string
= findMinimumString(S);
document.write(minimum_string);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
25 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...