Strange error for a c solution


  • 0
    S

    I was trying to solve this with a c program:

    the first issue I have is the strlen(s) did not return proper length for ''oneoneonezero" string
    then I change to count the "\0" for getting the length of the string.

    second issue is the test 16 pass when I run the program with the test case, but when submit, there is error.

    Anybody any idears?

    char* originalDigits(char* s) {
        /*
        The idea is we first hash s into a hashtable, everytime a char hit we +1 the value in the hashtable.
        This give us a table of how many times each letter is hit.
        Then we decode the table follow the below sequence.
        use z to find all the occurance of zero, we minus the occurance of z, e,r,o in the table with the occurence time of z(value in the hashtable for z)
        This will result in the number of zeros in the s.
        Then we do this for 2 and so on.
        The decode order:
        z-0 zero
        w-2 two
        u-4 four
        o-1 one
        f-5 five
        x-6 six
        s-7 seven
        r-3 three
        g-8 eight
        i-9 nine
        */
        unsigned int nums[10]={[0 ... 9] = 0};
        unsigned int hash[255]={[0 ... 254] = 0};
        int i=0;
        int count=0;
        while(s[count]!='\0'){
            count++;
        }
        for(i=0;i<count;i++){
            hash[s[i]]++;
        }
        //decode for zero
        nums[0]=hash['z'];
        hash['e']=hash['e']-hash['z'];
        hash['r']=hash['r']-hash['z'];
        hash['o']=hash['o']-hash['z'];
        hash['z'] = 0;
        
        
        //decode for two
        nums[2]=hash['w'];
        hash['t']=hash['t']-hash['w'];
        hash['o']=hash['o']-hash['w'];
        hash['w']=0;
        
        //decode for four
        nums[4]=hash['u'];
        hash['f']=hash['f']-hash['u'];
        hash['o']=hash['o']-hash['u'];
        hash['r']=hash['r']-hash['u'];
        hash['u']=0;
        
        //decode for one
        nums[1]=hash['o'];
        hash['n']=hash['n']-hash['o'];
        hash['e']=hash['e']-hash['o'];
        hash['o']=0;
        
        //decode for five
        nums[5]=hash['f'];
        hash['i']=hash['i']-hash['f'];
        hash['v']=hash['v']-hash['f'];
        hash['e']=hash['e']-hash['f'];
        hash['f']=0;
        
        //decode for six
        nums[6]=hash['x'];
        hash['s']=hash['s']-hash['x'];
        hash['i']=hash['i']-hash['x'];
        hash['x']=0;
        
        //decode for seven
        nums[7]=hash['s'];
        hash['e']=hash['e']-hash['s'];
        hash['v']=hash['v']-hash['s'];
        hash['e']=hash['e']-hash['s'];
        hash['n']=hash['n']-hash['s'];
        hash['s']=0;
        
        //decode for three
        nums[3]=hash['r'];
        hash['t']=hash['t']-hash['r'];
        hash['h']=hash['h']-hash['r'];
        hash['e']=hash['e']-hash['r'];
        hash['e']=hash['e']-hash['r'];
        hash['r']=0;
        
        //decode for eight
        nums[8]=hash['g'];
        hash['e']=hash['e']-hash['g'];
        hash['i']=hash['i']-hash['g'];
        hash['h']=hash['h']-hash['g'];
        hash['t']=hash['t']-hash['g'];
        hash['g']=0;
        
        //decode for i
        nums[9]=hash['i'];
        hash['n']=hash['n']-hash['i'];
        hash['n']=hash['n']-hash['i'];
        hash['e']=hash['e']-hash['i'];
        hash['i']=hash['i']-hash['i'];
    
        unsigned int length=0;
        for(i=0;i<10;i++){
            length+=nums[i];
        }
        
        char* ret=NULL;
        ret=(char*)malloc(sizeof(char)*length);
        length=0;
        for(i=0;i<10;i++){
            int j=0;
            for(j=0;j<nums[i];j++){
                ret[j+length]='0'+i;
            }
            length += nums[i];
        }
        
        return ret;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.