Javascript shortest & fastest solution // 56 ms // beats 100 %
\nvar countOfAtoms = function(formula) {\n    let [dic, coeff, stack, elem, cnt, j, res] = [{}, 1, [], "", 0, 0, ""];\n    for (let i = formula.length - 1; i >=
```\nvar countOfAtoms = function(formula) {\n    let [dic, coeff, stack, elem, cnt, j, res] = [{}, 1, [], "", 0, 0, ""];\n    for (let i = formula.length - 1; i >= 0; i--) {\n        if (!isNaN(formula[i] * 1)) {\n            cnt += Number(formula[i]) * (10 ** j), j += 1;\n        } \n        else if (formula[i] == ")") {\n            stack.push(cnt), coeff *= cnt, j = cnt = 0;\n        }\n        else if (formula[i] == "(") {\n            coeff = Math.floor(coeff / stack.pop()), j = cnt = 0;\n        }\n        else if (formula[i] == formula[i].toUpperCase()) {\n            elem += formula[i];\n            elem = elem.split("").reverse().join("");\n            dic[elem] = dic[elem] || 0;\n            dic[elem] += (cnt || 1) * coeff;\n            elem = "", j = cnt = 0;\n        }\n        else {\n            elem += formula[i];\n        }\n    }\n    Object.keys(dic).sort().forEach(function(c) {res += dic[c] > 1 ? c + String(dic[c]): c;});\n    return res;\n};\n```
[]