Given a long integer, return the smallest(magnitude) integer permutation of that number.
Examples:
Input : 5468001
Output : 1004568
Input : 5341
Output : 1345
Question Source : GE digital Interview Experience | Set 6
We have already discussed a solution in below post.
Smallest number by rearranging digits of a given number
In this post, a different approach is discussed.
Approach : As number is long, store the number as string, sort the string, if there is no leading zero, return this string, if there is any leading zero, swap first element of string with first non-zero element of string, and return the string.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
string findSmallestPermutation(string s)
{
int len = s.length();
sort(s.begin(), s.end());
int i = 0;
while (s[i] == '0' )
i++;
swap(s[0], s[i]);
return s;
}
int main()
{
string s = "5468001" ;
string res = findSmallestPermutation(s);
cout << res << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static char [] findSmallestPermutation(String s1)
{
char s[] = s1.toCharArray();
Arrays.sort(s);
int i = 0 ;
while (s[i] == '0' )
i++;
char temp = s[ 0 ];
s[ 0 ] = s[i];
s[i] = temp;
return s;
}
public static void main(String args[])
{
String s = "5468001" ;
char res[] = findSmallestPermutation(s);
System.out.println(res);
}
}
|
Python
def sort_string(a):
return ''.join( sorted (a))
def findSmallestPermutation(s):
s = sort_string(s)
i = 0
while (s[i] = = '0' ):
i + = 1
a = list (s)
temp = a[ 0 ]
a[ 0 ] = a[i]
a[i] = temp
s = "".join(a)
return s
s = "5468001"
res = findSmallestPermutation(s)
print res
|
C#
using System;
public class GFG {
static char [] findSmallestPermutation( string s1)
{
char []s = s1.ToCharArray();;
Array.Sort(s);
int i = 0;
while (s[i] == '0' )
i++;
char temp = s[0];
s[0] = s[i];
s[i] = temp;
return s;
}
public static void Main()
{
string s = "5468001" ;
char []res = findSmallestPermutation(s);
Console.WriteLine(res);
}
}
|
PHP
<?php
function findSmallestPermutation( $s )
{
$len = strlen ( $s );
$s = str_split ( $s );
sort( $s );
$i = 0;
while ( $s [ $i ] == '0' )
$i ++;
$tmp = $s [0];
$s [0] = $s [ $i ];
$s [ $i ] = $tmp ;
$s =implode( "" , $s );
return $s ;
}
$s = "5468001" ;
$res = findSmallestPermutation( $s );
echo $res ;
?>
|
Javascript
function findSmallestPermutation(s)
{
let len = s.length;
s = s.split( "" );
s = s.sort();
let i = 0;
while (s[i] == '0' )
i++;
let tmp = s[0];
s[0] = s[i];
s[i] = tmp;
s= s.join( "" );
return s;
}
let s = "5468001" ;
let res = findSmallestPermutation(s);
document.write(res);
|
Output:
1004568
Optimization :
Since character set is limited (‘0’ to ‘9’), we can write our own sort method that works in linear time (by counting frequencies of all characters)
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.