Brute Force | Easy
\n\n\n# Code\n\nclass Solution {\n\n    int getNumeric(string &formula , int &i){\n        int j = i;\n        while(i<formula.size()&&formula[i]>=\'0\'&&formul
\n\n\n# Code\n```\nclass Solution {\n\n    int getNumeric(string &formula , int &i){\n        int j = i;\n        while(i<formula.size()&&formula[i]>=\'0\'&&formula[i]<=\'9\'){\n            i++;\n        }\n        if(j==i){\n            return 1;\n        }\n        return stoi(formula.substr(j,i-j));\n    }\n\n    void modifyStack(stack<pair<string,int>>&st , int &count){\n        queue<pair<string,int>> q;\n        while(!st.empty()&&st.top().first != "("){\n            auto p = st.top();\n            p.second = p.second * count;\n            q.push(p);\n            st.pop();\n        }\n        st.pop();\n        while(!q.empty()){\n            st.push(q.front());\n            q.pop();\n        }\n    }\n\n    string getElement(string &formula , int &i){\n        int j = i+1;\n        while(j<formula.size()&&formula[j]>=\'a\'&&formula[j]<=\'z\'){\n            j++;\n        }\n        string element =  formula.substr(i,j-i);\n        i = j;\n        return element;\n    }\n\npublic:\n    string countOfAtoms(string formula) {\n        stack<pair<string,int>> st;\n        for(int i = 0 ; i < formula.size() ;){\n            if(formula[i]>=\'A\'&&formula[i]<=\'Z\'){\n                string ele = getElement(formula,i);\n                int count = getNumeric(formula,i);\n                st.push({ele,count});\n            }else if(formula[i]==\'(\'){\n                st.push({"(",1});\n                i++;\n            }else if(formula[i]==\')\'){\n                int j = i + 1;\n                int count = getNumeric(formula,j);\n                modifyStack(st,count);\n                i=j;\n            }\n        }\n        map<string,int> res;\n        while(!st.empty()){\n            res[st.top().first] += st.top().second;\n            st.pop();\n        }\n        string ans = "";\n        for(auto ec : res){\n            ans += ec.first;\n            if(ec.second>1) ans += to_string(ec.second);\n        }\n        return ans;\n        \n    }\n};\n```
['C++']