[C++] Short stack + map
\nclass Solution {\npublic:\n    string countOfAtoms(string formula) {\n        vector<pair<string, int> > st;\n        for(int i = 0;i < formula.size();i++) {\
```\nclass Solution {\npublic:\n    string countOfAtoms(string formula) {\n        vector<pair<string, int> > st;\n        for(int i = 0;i < formula.size();i++) {\n            char c = formula[i];\n            if (c == \'(\') \n                st.push_back(make_pair("(", 1));\n            \n            if (\'A\' <= c && c <= \'Z\') {\n                string atom = readAtomName(i, formula);\n                int cnt = readDigits(i, formula);\n                st.push_back(make_pair(atom, cnt));\n            }\n            \n            if (c == \')\') {\n                int cnt = readDigits(i, formula);\n                // update atoms inside parentheses\n                int j = st.size() - 1;\n                while (st[j].first != "(" || !st[j].second) // first available "("\n                    st[j--].second *= cnt;\n                st[j].second--;\n            }\n        }\n        // accumulate results\n        map<string, int> atoms;\n        for(auto e : st) \n            if (e.first != "(") \n                atoms[e.first] += e.second;\n        \n        // format the output\n        string output = "";\n        for(auto it = atoms.begin(); it != atoms.end(); it++) {\n            output.append(it->first);\n            if (it->second == 1) continue;\n            output.append(to_string(it->second));\n        }\n        return output;\n    }\n    \n    int readDigits(int &i, string &formula) {\n        int cnt = 0;\n        while (i + 1 < formula.size() && isdigit(formula[i+1])) \n            cnt = cnt * 10 + formula[++i] - \'0\';\n        return max(1, cnt);\n    }\n    \n    string readAtomName(int &i, string &formula) {\n        string atom = string(1, formula[i]);\n        while (i + 1 < formula.size() && \'a\' <= formula[i+1] && formula[i+1] <= \'z\') \n            atom.push_back(formula[++i]);\n        return atom;\n    }\n};\n```
['Stack', 'C']