Reorder characters of a string to valid English representations of digits | Java program to implement the above approach ; Function to construct the original set of digits from the string in ascending order ; Store the unique characters corresponding to word and number ; Store the required result ; Store the frequency of each character of S ; Traverse the unique characters ; Store the count of k [ i ] in S ; Traverse the corresponding word ; Decrement the frequency of characters by x ; Append the digit x times to ans ; Sort the digits in ascending order ; Driver Code ; Given string , s ; Function Call
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String construct_digits ( String s ) { char [ ] k = { ' z ' , ' w ' , ' u ' , ' x ' , ' g ' , ' h ' , ' o ' , ' f ' , ' v ' , ' i ' } ; String [ ] l = { " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " } ; int [ ] c = { 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 } ; List < Integer > ans = new ArrayList < > ( ) ; HashMap < Character , Integer > d = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { d . put ( s . charAt ( i ) , d . getOrDefault ( s . charAt ( i ) , 0 ) + 1 ) ; } for ( int i = 0 ; i < k . length ; i ++ ) { int x = 0 ; if ( d . containsKey ( k [ i ] ) ) x = d . get ( k [ i ] ) ; for ( int j = 0 ; j < l [ i ] . length ( ) ; j ++ ) { if ( d . containsKey ( l [ i ] . charAt ( j ) ) ) d . put ( l [ i ] . charAt ( j ) , d . get ( l [ i ] . charAt ( j ) ) - x ) ; } for ( int j = 0 ; j < x ; j ++ ) ans . add ( c [ i ] ) ; } Collections . sort ( ans ) ; String str = " " ; for ( int val : ans ) str += val ; return str ; } public static void main ( String [ ] args ) { String s = " fviefuro " ; System . out . println ( construct_digits ( s ) ) ; } }