Approach #2: Stack [Accepted] in C++ | Iterative
Please respect my time\n\n\n\nclass Solution {\npublic:\n    string countOfAtoms(string formula) {\n        int n = formula.size();\n        stack<map<string,in
#### Please respect my time\n\n```\n\nclass Solution {\npublic:\n    string countOfAtoms(string formula) {\n        int n = formula.size();\n        stack<map<string,int>> stack;\n        map<string,int> test;\n        stack.push(test);\n\n\n        for(int i=0;i<n;){\n          if(formula[i]==\'(\'){\n            map<string,int> test;\n            stack.push(test);\n            i++;\n          }\n          else if(formula[i]==\')\'){\n            auto top = stack.top(); stack.pop();\n            int iStart = ++i;\n            //making number here\n            while(i<n && isdigit(formula[i])) i++;\n            int number = i>iStart ? stoi(formula.substr(iStart,i-iStart)) : 1;\n            for(auto &curr : top){\n              stack.top()[curr.first] += curr.second*number;\n            }\n          }\n          else{\n            int iStart = i++;\n            //making formula here\n            while(i<n && islower(formula[i])) i++;\n            string name = formula.substr(iStart,i-iStart);\n            iStart = i;\n            //making number here\n            while(i<n && isdigit(formula[i])) i++;\n            int number = i>iStart ? stoi(formula.substr(iStart,i-iStart)) : 1;\n            stack.top()[name] += number;\n          }\n        }\n\n\n        string res;\n        auto top = stack.top();\n        for(auto &curr : top){\n          res += curr.first;\n          int freq = curr.second;\n          if(freq>1)  res += to_string(freq);\n        }\n        return res;\n    }\n};\n```
['Stack', 'Iterator']