This Took me about 12hrs to Code | 4ms runtime, 6.5 mb ram | Code in C.
Intuition\nIDK. My Brain is fried. If you can Try to understand by reading the code. Becoz I\'m too tired to describe my code.\n\nOk, I\'ll explain a bit.\n- I 
# Intuition\nIDK. My Brain is fried. If you can Try to understand by reading the code. Becoz I\'m too tired to describe my code.\n\nOk, I\'ll explain a bit.\n- I Created a structure to store atom and its number.\n- Then Read the formula till I hit \'(\'.\n- If I hit \'(\' I sent the formula and index at the start of the next atom after \'(\' recursionally.\n- return when i see \')\'.\n- But before returning I multiply all the all the individual atom numbers present inside the () with the number that\'s after \')\'.\n- sort the struct using qsort.\n- Read all the values of struct to an array.\n- Return the array (and your sanity) to LeetCode.\n# Code\n```\ntypedef struct count_of_atoms{\n    char atom[4];\n    int count;\n}count_of_atoms;\n\nint compair_all_atoms(const void*x, const void*y){\n    return strcmp(((count_of_atoms*)x)->atom, ((count_of_atoms*)y)->atom);\n}\n\nvoid insert_element(count_of_atoms**current_atomsptr, int* atom_typeptr, char *atom, int atom_number){\n    if(atom_number==0){atom_number++;}\n    if(atom[0]==0){return;}\n\n    count_of_atoms* current_atoms = *current_atomsptr;\n    for(int i=0; i<(*atom_typeptr); i++){\n        if(strcmp(current_atoms[i].atom,atom)==0){\n            current_atoms[i].count +=atom_number;\n            return;\n        }\n    }\n    (*atom_typeptr)++;\n    current_atoms = realloc(current_atoms, (*atom_typeptr)*sizeof(count_of_atoms));\n    strcpy(current_atoms[(*atom_typeptr)-1].atom, atom);\n    current_atoms[(*atom_typeptr)-1].count = atom_number;\n    *current_atomsptr = current_atoms;\n}\n\nint current_formula(count_of_atoms** current_atomsptr, int *structSize_ptr, char *formula, int *index_ptr){\n    count_of_atoms* current_atoms = * current_atomsptr;\n    int atom_type = 0;\n    int index = *index_ptr;\n    int atom_name_index=0;\n    char atom_name[4] = {};\n    int atom_number = 0;\n    while(formula[index]!=\'\\0\'){\n        if(formula[index]>=\'A\' && formula[index]<=\'Z\'){\n            insert_element(&current_atoms, &atom_type, atom_name, atom_number);\n            atom_name_index =0;\n            atom_name[atom_name_index++] = formula[index];\n            atom_name[1] = 0;\n            atom_name[2] = 0;\n            atom_name[3] = 0;\n            atom_number = 0;\n        }\n        else if(formula[index]>=\'a\' && formula[index]<=\'z\'){\n            atom_name[atom_name_index++] = formula[index];\n        }\n        else if(formula[index]>=\'0\' && formula[index]<=\'9\'){\n            atom_number *= 10;\n            atom_number += formula[index] - \'0\';\n        }\n        else if(formula[index]==\'(\'){\n            count_of_atoms * new_atoms = malloc(sizeof(count_of_atoms));\n            int new_atom_type = 0;\n            index++;\n            current_formula(&new_atoms, &new_atom_type, formula, &index);\n\n            for(int i=0; i<new_atom_type; i++){\n                insert_element(&current_atoms, &atom_type, new_atoms[i].atom, new_atoms[i].count);\n            }\n            free(new_atoms);\n        }\n        else if(formula[index]==\')\'){\n            insert_element(&current_atoms, &atom_type, atom_name, atom_number);\n            (*structSize_ptr) += atom_type;\n\n            int multiplier = 0;\n            while(formula[index+1]>=\'0\' && formula[index+1]<=\'9\')\n            {\n                multiplier*=10;\n                multiplier += formula[++index]-\'0\';\n            }\n            if(multiplier>1){\n                for(int i=0; i<atom_type; i++) {current_atoms[i].count*=multiplier;}\n            }\n            *index_ptr = index;\n            * current_atomsptr = current_atoms;\n            return 0;\n        }\n        index++;\n    }\n    insert_element(&current_atoms, &atom_type, atom_name, atom_number);\n    *index_ptr = index;\n    (*structSize_ptr) += atom_type;\n    * current_atomsptr = current_atoms;\n    return 0;\n}\n\nvoid intToStr(int num, char *str) {\n    sprintf(str, "%d", num);\n}\n\nchar* countOfAtoms(char* formula) {\n    count_of_atoms *all_atoms = malloc(sizeof(count_of_atoms));\n    int structSize = 0;\n    int index = 0;\n\n    current_formula(&all_atoms, &structSize, formula, &index);\n    qsort(all_atoms, structSize, sizeof(count_of_atoms), compair_all_atoms);\n\n    char *arr = malloc(1000*sizeof(char));\n    int arrptr=0;\n    for(int i=0; i<structSize; i++){\n        strncpy(&arr[arrptr], all_atoms[i].atom, strlen(all_atoms[i].atom));\n        arrptr +=strlen(all_atoms[i].atom);\n\n        if(all_atoms[i].count>1){\n            char num[12];\n            intToStr(all_atoms[i].count, num);\n            strncpy(arr+arrptr, num, strlen(num));\n            arrptr +=strlen(num);\n        }\n    }\n    arr[arrptr] = \'\\0\';\n    free(all_atoms);\n    return arr;\n}\n```
['C']