Header Ad

Leetcode Reconstruct Original Digits from English problem solution

In this Leetcode Reconstruct Original Digits from English problem solution, we have given a string s containing an out-of-order English representation of digits 0-9, return the digits in ascending order.

Leetcode Reconstruct Original Digits from English problem solution


Problem solution in Python.

def originalDigits(self, s: str) -> str:
	
    lst = {'z':0, 'o':0, 'w':0, 't':0, 'u':0, 'f':0, 'x':0, 's':0, 'g':0, 'i':0}
    for i in s:
        if i in lst:
            lst[i] += 1
    lst['o'] -= (lst['z'] + lst['w'] + lst['u'])
    lst['t'] -= (lst['w'] + lst['g']) 
    lst['f'] -= lst['u']
    lst['s'] -= lst['x']
    lst['i'] -= (lst['f'] + lst['x'] + lst['g'])
    
    answer = ""
    for i, x in enumerate(lst.values()):
        answer += (str(i) * x)
    return answer



Problem solution in Java.

class Solution {
    public String originalDigits(String s) {
        
        int[] count = new int[26];
        int[] digits = new int[10];
        StringBuilder str = new StringBuilder ();
        
        for (char c : s.toCharArray ()) {
            ++count[c - 'a'];
        }
        
        digits[0] = count[25];
        digits[2] = count[22];
        digits[4] = count[20];
        digits[6] = count[23];
        digits[8] = count[6];
        digits[1] = count[14] - digits[0] - digits[2] - digits[4];
        digits[3] = count[7] - digits[8];
        digits[5] = count[5] - digits[4];
        digits[7] = count[18] - digits[6];
        digits[9] = count[8] - digits[5] - digits[6] - digits[8];
        
        for (int i = 0; i < 10; i++) {
            while (digits[i]-- != 0) {
                str.append ((char) (i + 48));
            }
        }
        
        return str.toString ();
    }
}


Problem solution in C++.

class Solution {
public:
   string originalDigits(string s) {
       vector<int> op(10, 0);
       vector<int> table(26, 0);
       
       int n = s.length();
       for(int i=0; i<n; i++) table[s[i]-'a']++;

       table['e'-'a']-=table['g'-'a'];
       table['i'-'a']-=table['g'-'a'];
       table['h'-'a']-=table['g'-'a'];
       table['t'-'a']-=table['g'-'a'];
       op[8]+=table['g'-'a'];
       table['g'-'a']=0;

       table['s'-'a']-=table['x'-'a'];
       table['i'-'a']-=table['x'-'a'];
       op[6]+=table['x'-'a'];
       table['x'-'a']=0;

       table['e'-'a']-=table['z'-'a'];
       table['r'-'a']-=table['z'-'a'];
       table['o'-'a']-=table['z'-'a'];
       op[0]+=table['z'-'a'];
       table['z'-'a']=0;

       table['t'-'a']-=table['w'-'a'];
       table['o'-'a']-=table['w'-'a'];
       op[2]+=table['w'-'a'];
       table['w'-'a']=0;

       table['f'-'a']-=table['u'-'a'];
       table['o'-'a']-=table['u'-'a'];
       table['r'-'a']-=table['u'-'a'];
       op[4]+=table['u'-'a'];
       table['u'-'a']=0;

       table['n'-'a']-=table['o'-'a'];
       table['e'-'a']-=table['o'-'a'];
       op[1]+=table['o'-'a'];
       table['o'-'a']=0;

       table['i'-'a']-=table['f'-'a'];
       table['v'-'a']-=table['f'-'a'];
       table['e'-'a']-=table['f'-'a'];
       op[5]+=table['f'-'a'];
       table['f'-'a']=0;

       table['s'-'a']-=table['v'-'a'];
       table['e'-'a']-=2*table['v'-'a'];
       table['n'-'a']-=table['v'-'a'];
       op[7]+=table['v'-'a'];
       table['v'-'a']=0;

       table['n'-'a']-=2*table['i'-'a'];
       table['e'-'a']-=table['i'-'a'];
       op[9]+=table['i'-'a'];
       table['i'-'a']=0;
       
       op[3]+=table['t'-'a'];
       
       string ans="";
       for(int i=0;i<10;i++) {
           ans+=string(op[i], '0'+i);
       }
       return ans;
   }
};


Problem solution in C.

#define SIZE 12000
void function(int *map, int num, char* s){
    int len=strlen(s);
    for(int i=0;i<len;i++){
        map[s[i]-'a']-=num;
    }
}
char* originalDigits(char* s) {
    int *map=(int*)calloc(26,sizeof(int));
    int len=strlen(s);
    for(int i=0;i<len;i++){
        map[s[i]-'a']++;
    }
    char *array[]={
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
    };
    char *ret=(char*)malloc(SIZE*sizeof(char));
    int count=0;
    int *nums=(int*)calloc(10,sizeof(int));
    nums[0]=map['z'-'a'];
    function(map,nums[0],array[0]);
    nums[2]=map['w'-'a'];
    function(map,nums[2],array[2]);
    nums[4]=map['u'-'a'];
    function(map,nums[4],array[4]);
    nums[6]=map['x'-'a'];
    function(map,nums[6],array[6]);
    nums[8]=map['g'-'a'];
    function(map,nums[8],array[8]);
    nums[1]=map['o'-'a'];
    function(map,nums[1],array[1]);
    nums[3]=map['t'-'a'];
    function(map,nums[3],array[3]);
    nums[5]=map['f'-'a'];
    function(map,nums[5],array[5]);
    nums[7]=map['s'-'a'];
    function(map,nums[7],array[7]);
    nums[9]=map['i'-'a'];
    function(map,nums[9],array[9]);
    for(int i=0;i<10;i++){
        for(int j=0;j<nums[i];j++){
             ret[count++]=i+'0';
        }
    }
    ret[count++]='\0';
    return ret;
}


Post a Comment

0 Comments