Find smallest possible Number from a given large Number with same count of digits | Java implementation of the above approach ; Function for finding the smallest possible number after swapping the digits any number of times ; Variable to store the final answer ; Array to store the count of occurrence of each digit ; Loop to calculate the number of occurrences of every digit ; Loop to get smallest number ; Returning the answer ; Driver code
class GFG { static String smallestPoss ( String s , int n ) { String ans = " " ; int arr [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + String . valueOf ( i ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 ; String K = "325343273113434" ; System . out . print ( smallestPoss ( K , N ) ) ; } }