C++ iterative solution
\nclass Solution {\npublic:\n    string countOfAtoms(string f) {\n        stack<map<string,int>> st;\n        map<string, int> cur;\n        for (int i = 0; i <
```\nclass Solution {\npublic:\n    string countOfAtoms(string f) {\n        stack<map<string,int>> st;\n        map<string, int> cur;\n        for (int i = 0; i < f.size();) {\n            if (f[i] == '(') {\n                st.push(move(cur));\n                cur = map<string,int>();\n                ++i;\n            }\n            else if (f[i] == ')') {\n                int j = i+1;\n                int num = 0;\n                while (j < f.size() && isdigit(f[j])) num = num*10 + (f[j++] - '0');\n                num = max(num,1);\n                for (auto p:cur) st.top()[p.first] += p.second * num;\n                cur = move(st.top());\n                st.pop();\n                i = j;\n            }\n            else {\n                int j = i + 1;\n                while (j < f.size() && f[j] >= 'a' && f[j] <= 'z') ++j;\n                auto name = f.substr(i,j-i);\n                int num = 0;\n                while (j < f.size() && isdigit(f[j])) num = num*10 + (f[j++] - '0');\n                num = max(num,1);\n                cur[name] += num;\n                i = j;\n            }\n        }\n        string ans;\n        for (auto p:cur) {\n            ans += p.first;\n            if (p.second > 1) ans += to_string(p.second);\n        }\n        \n        return ans;\n    }\n};\n```
[]