✅Beats 100% with Proof (Screenshot) - Easy Approach 😎⭐⭐⭐ C++
Intuition\n\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim
# Intuition\n![100.png](https://assets.leetcode.com/users/images/2da8bcdd-9e59-414a-8ad8-6a4e98ad6c31_1720926906.467354.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    string countOfAtoms(string formula) {\n        stack<unordered_map<string, int>> stk;\n        stk.push({}); // Initialize with an empty map\n        \n        int i = 0, n = formula.length();\n        \n        while (i < n) {\n            if (formula[i] == \'(\') {\n                stk.push({});\n                i++;\n            } else if (formula[i] == \')\') {\n                unordered_map<string, int> top = stk.top();\n                stk.pop();\n                i++;\n                int start = i;\n                while (i < n && isdigit(formula[i])) {\n                    i++;\n                }\n                int multiplier = start < i ? stoi(formula.substr(start, i - start)) : 1;\n                for (auto& kv : top) {\n                    stk.top()[kv.first] += kv.second * multiplier;\n                }\n            } else {\n                int start = i++;\n                while (i < n && islower(formula[i])) {\n                    i++;\n                }\n                string element = formula.substr(start, i - start);\n                start = i;\n                while (i < n && isdigit(formula[i])) {\n                    i++;\n                }\n                int count = start < i ? stoi(formula.substr(start, i - start)) : 1;\n                stk.top()[element] += count;\n            }\n        }\n        \n        unordered_map<string, int> result = stk.top();\n        vector<string> elements;\n        for (auto& kv : result) {\n            elements.push_back(kv.first);\n        }\n        sort(elements.begin(), elements.end());\n        \n        string res;\n        for (const auto& element : elements) {\n            res += element;\n            if (result[element] > 1) {\n                res += to_string(result[element]);\n            }\n        }\n        \n        return res;\n    }\n};\n```
['C++']