Given a numeric string str, the task is to find the smallest integer that can be formed by swapping adjacent digits of distinct parity. Examples:
Input: 836360 Output: 338660 Explanation: 1st Swap: 836360 -> 386360 2nd Swap: 386360 -> 383660 3rd Swap: 383660 -> 338660 Input: 1003 Output: 13
Approach: We can observe that on repeated swaps, we can split the even and odd digits of str into two separate blocks. The order of digits in their respective blocks will be the same as their order of appearance in the string as swapping within a block (same parity) is not allowed. Thus, after separating str into two separate blocks, we need to traverse these two blocks and append the minimum of the two values currently pointed, to the answer. The final string generated after this operation followed by the removal of leading 0’s, if any, is the required answer. Example: The even and odd blocks int the order of their appearance in 836360 are {8, 6, 6, 0} and {3, 3} respectively. Hence the smallest number ans formed is as follows:
- ans = ans + min(8, 3) => ans = 3
- ans = ans + min(8, 3) => ans = 33
- Since, all the odd digits are exhausted, the remaining even digits need to be added one by one.
- Hence, the required answer is 338660
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string findAns(string s)
{
int digit;
vector< int > odd;
vector< int > even;
for ( auto c : s) {
digit = c - '0' ;
if (digit & 1)
odd.push_back(digit);
else
even.push_back(digit);
}
int i = 0;
int j = 0;
string ans = "" ;
while (i < odd.size()
and j < even.size()) {
if (odd[i] < even[j])
ans += ( char )(odd[i++] + '0' );
else
ans += ( char )(even[j++] + '0' );
}
while (i < odd.size())
ans += ( char )(odd[i++] + '0' );
while (j < even.size())
ans += ( char )(even[j++] + '0' );
while (ans[0] == '0' ) {
ans.erase(ans.begin());
}
return ans;
}
int main()
{
string s = "894687536" ;
cout << findAns(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static String findAns(String s)
{
int digit;
Vector<Integer> odd = new Vector<Integer>();
Vector<Integer> even = new Vector<Integer>();
for ( char c : s.toCharArray())
{
digit = c - '0' ;
if (digit % 2 == 1 )
odd.add(digit);
else
even.add(digit);
}
int i = 0 ;
int j = 0 ;
String ans = "" ;
while (i < odd.size() && j < even.size())
{
if (odd.get(i) < even.get(j))
ans += ( char )(odd.get(i++) + '0' );
else
ans += ( char )(even.get(j++) + '0' );
}
while (i < odd.size())
ans += ( char )(odd.get(i++) + '0' );
while (j < even.size())
ans += ( char )(even.get(j++) + '0' );
while (ans.charAt( 0 ) == '0' )
{
ans = ans.substring( 1 );
}
return ans;
}
public static void main(String[] args)
{
String s = "894687536" ;
System.out.print(findAns(s));
}
}
|
Python3
def findAns(s):
odd = []
even = []
for c in s:
digit = int (c)
if (digit & 1 ):
odd.append(digit)
else :
even.append(digit)
i = 0
j = 0
ans = ""
while (i < len (odd) and j < len (even)):
if (odd[i] < even[j]):
ans + = str (odd[i])
i = i + 1
else :
ans + = str (even[j])
j = j + 1
while (i < len (odd)):
ans + = str (odd[i])
i = i + 1
while (j < len (even)):
ans + = str (even[j])
j = j + 1
while (ans[ 0 ] = = '0' ):
ans = ans[ 1 :]
return ans
s = "894687536"
print (findAns(s))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static String findAns(String s)
{
int digit;
List< int > odd = new List< int >();
List< int > even = new List< int >();
foreach ( char c in s.ToCharArray())
{
digit = c - '0' ;
if (digit % 2 == 1)
odd.Add(digit);
else
even.Add(digit);
}
int i = 0;
int j = 0;
String ans = "" ;
while (i < odd.Count && j < even.Count)
{
if (odd[i] < even[j])
ans += ( char )(odd[i++] + '0' );
else
ans += ( char )(even[j++] + '0' );
}
while (i < odd.Count)
ans += ( char )(odd[i++] + '0' );
while (j < even.Count)
ans += ( char )(even[j++] + '0' );
while (ans[0] == '0' )
{
ans = ans.Substring(1);
}
return ans;
}
public static void Main(String[] args)
{
String s = "894687536" ;
Console.Write(findAns(s));
}
}
|
Javascript
<script>
function findAns( s) {
var digit;
var odd = [];
var even = [];
for ( var i =0;i<s.length;i++) {
digit = s[i].charCodeAt(0) - '0' .charCodeAt(0);
if (digit % 2 == 1)
odd.push(digit);
else
even.push(digit);
}
var i = 0;
var j = 0;
var ans = "" ;
while (i < odd.length && j < even.length) {
if (odd[i] < even[j])
ans += String.fromCharCode(odd[i++] + '0' .charCodeAt(0));
else
ans += String.fromCharCode(even[j++] + '0' .charCodeAt(0));
}
while (i < odd.length)
ans += String.fromCharCode(odd[i++] + '0' .charCodeAt(0));
while (j < even.length)
ans += String.fromCharCode(even[j++] + '0' .charCodeAt(0));
while (ans.charAt(0) == '0') {
ans = ans.substring(1);
}
return ans;
}
var s = "894687536" ;
document.write(findAns(s));
</script>
|
Time Complexity: O(N), where N is the size of the given string.