Neat implementation using stack of map
\n\n# Code\n\nclass Solution {\n    bool iscapital(char &c){\n        return (c>=\'A\'&&c<=\'Z\');\n    }\n    bool isno(char &c){\n        return (c>=\'0\'&&c<
\n\n# Code\n```\nclass Solution {\n    bool iscapital(char &c){\n        return (c>=\'A\'&&c<=\'Z\');\n    }\n    bool isno(char &c){\n        return (c>=\'0\'&&c<=\'9\');\n    }\n    bool ischaracter(char &c){\n        return (c>=\'a\'&&c<=\'z\');\n    }\n    string getelement(int &i, string &formula){\n        string res=""; int n = formula.size();\n        res.push_back(formula[i]); i++;\n        while(i<n&&ischaracter(formula[i])){\n            res.push_back(formula[i]);\n            i++;\n        }\n        return res;\n    }\n    int getno(int &i, string&formula){\n        string no = ""; int n = formula.size();\n        while(i<n&&isno(formula[i])){\n            no.push_back(formula[i]); i++;\n        }\n        if(no.size()) return stoi(no);\n        return 1;\n    }\n    void addmap(stack<map<string,int>>&s){\n        map<string,int>mp;\n        s.push(mp);\n    }\n    void removemap(stack<map<string,int>>&s,int no){\n        map<string,int>mp = s.top();\n        s.pop();\n        for(auto&[k,v]:mp){\n            s.top()[k]+=(v*no);\n        }\n    }\npublic:\n    string countOfAtoms(string formula) {\n        stack<map<string,int>>s;\n        int n= formula.size();\n        int i = 0;\n        map<string,int>f; s.push(f);\n        while(i<n){\n            if(iscapital(formula[i])){\n                string element = getelement(i,formula);\n                int no = getno(i,formula);\n                s.top()[element]+=no;\n            }\n            if(formula[i]==\'(\'){\n                addmap(s);i++;\n            }\n            else if(formula[i]==\')\'){\n                i++;\n                int no = getno(i,formula);\n                removemap(s,no);\n            }\n        }\n        string ans = "";\n        for(auto&[k,v]:s.top()){\n            ans+=k;\n            if(v>1){\n                ans+=to_string(v);\n            }\n        }\n        return ans;\n    }\n};\n```
['Hash Table', 'Stack', 'C++']