Reorder characters of a string to valid English representations of digits | Python 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 ; Given string , s ; Function Call
from collections import Counter NEW_LINE def construct_digits ( s ) : NEW_LINE INDENT k = [ " z " , " w " , " u " , " x " , " g " , " h " , " o " , " f " , " v " , " i " ] NEW_LINE l = [ " zero " , " two " , " four " , " six " , " eight " , " three " , " one " , " five " , " seven " , " nine " ] NEW_LINE c = [ 0 , 2 , 4 , 6 , 8 , 3 , 1 , 5 , 7 , 9 ] NEW_LINE ans = [ ] NEW_LINE d = Counter ( s ) NEW_LINE for i in range ( len ( k ) ) : NEW_LINE INDENT x = d . get ( k [ i ] , 0 ) NEW_LINE for j in range ( len ( l [ i ] ) ) : NEW_LINE INDENT d [ l [ i ] [ j ] ] -= x NEW_LINE DEDENT ans . append ( str ( c [ i ] ) * x ) NEW_LINE DEDENT ans . sort ( ) NEW_LINE return " " . join ( ans ) NEW_LINE DEDENT s = " fviefuro " NEW_LINE print ( construct_digits ( s ) ) NEW_LINE