We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This one is my solution. Please let me know if anything that i can do better here.
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>#define RANGE 10#define ASCII_RANGE 26charascii_table_array[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};/**@param digit - the digit of character or number from input string@return 1 is true else 0 if fails*/intisChar(intdigit){for(inti=0;i<ASCII_RANGE;i++){if(ascii_table_array[i]==digit){return1;}}return0;}voidaddToArray(intitem,int*doneArray){intemptyIdx=0;for(inti=0;i<100;i++){if(doneArray[i]==0){emptyIdx=i;break;}}doneArray[emptyIdx]=item;}// returns 1 if any match found else returns 0intisAlreadyDone(intitem,int*doneArray){for(inti=0;i<100;i++){if(doneArray[i]==item){return1;}}return0;}int*getResult(char*input){intcounter=0;inttempIdx=0;int*result_array=(int*)malloc(sizeof(int)*10);int*doneArray=(int*)malloc(sizeof(int)*100);intinputRange=strlen(input);for(inti=0;i<inputRange;i++){if(isChar(input[i])){// result_array[i] = 0;continue;}counter++;tempIdx=i;for(intj=0;j<inputRange;j++){if(isAlreadyDone(input[tempIdx],doneArray)==0){if(tempIdx>j){if(input[tempIdx]==input[j]){counter++;}}}}if(input[tempIdx]=='0'){result_array[0]=counter;addToArray(0,doneArray);}elseif(input[tempIdx]=='1'){result_array[1]=counter;addToArray(1,doneArray);}elseif(input[tempIdx]=='2'){result_array[2]=counter;addToArray(2,doneArray);}elseif(input[tempIdx]=='3'){result_array[3]=counter;addToArray(3,doneArray);}elseif(input[tempIdx]=='4'){result_array[4]=counter;addToArray(4,doneArray);}elseif(input[tempIdx]=='5'){result_array[5]=counter;addToArray(5,doneArray);}elseif(input[tempIdx]=='6'){result_array[6]=counter;addToArray(6,doneArray);}elseif(input[tempIdx]=='7'){result_array[7]=counter;addToArray(7,doneArray);}elseif(input[tempIdx]=='8'){result_array[8]=counter;addToArray(8,doneArray);}elseif(input[tempIdx]=='9'){result_array[9]=counter;addToArray(9,doneArray);}counter=0;// reseting the coutnertempIdx=0;// reseting the tempIdx}returnresult_array;}intmain(){char*input=(char*)malloc(sizeof(char)*10000);scanf("%s",input);int*result=getResult(input);for(inti=0;i<RANGE;i++){printf("%d ",result[i]);}free(input);free(result);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Digit Frequency
You are viewing a single comment's thread. Return to all comments →
This one is my solution. Please let me know if anything that i can do better here.