C++ | | Easy to Understand | | Unique sol ✅✅🔥
Intuition\n- Utilize a stack to manage nested element counts.\n- Traverse the formula character by character.\n- Update counts for elements based on their occur
# Intuition\n- Utilize a stack to manage nested element counts.\n- Traverse the formula character by character.\n- Update counts for elements based on their occurrence.\n- Sort and format element counts into the desired output string.\n# Approach\n\n1. **Parsing with a Stack**: \n   - Use a stack to handle nested parentheses. Each time an opening parenthesis `(` is encountered, push the current `EleCount` map onto the stack and start a new `EleCount` for the nested structure.\n   - When encountering a closing parenthesis `)`, pop the top of the stack (which represents the previous `EleCount` before the nested structure) and merge it with the current `EleCount` after applying any multiplier associated with the parentheses.\n\n2. **Counting Elements**:\n   - Traverse through the formula character by character.\n   - Identify elements (starting with uppercase letters, followed optionally by lowercase letters) and their associated counts (digits following the element).\n   - Update the `EleCount` map accordingly.\n\n3. **Formatting the Result**:\n   - Convert the `EleCount` map to a sorted vector of pairs (elements sorted in dictionary order).\n   - Build the result string by appending each element along with its count if greater than 1.\n\n# Complexity\n- **Time Complexity**: $$O(n)$$\n- **Space Complexity**:  $$O(n)$$\n\n# Code\n```cpp\nclass Solution {\npublic:\n    unordered_map<string, int> FuncCount(string formula) {\n        unordered_map<string, int> EleCount;\n        stack<unordered_map<string, int>> stk;\n        int n = formula.length();\n        int i = 0;\n\n        while (i < n) {\n            if (formula[i] == \'(\') {\n                stk.push(EleCount);\n                EleCount.clear();\n                i++;\n            } else if (formula[i] == \')\') {\n                i++;\n                int count = 0;\n                while (i < n && isdigit(formula[i])) {\n                    count = count * 10 + (formula[i] - \'0\');\n                    i++;\n                }\n                if (count == 0)\n                    count = 1;\n                unordered_map<string, int> temp = EleCount;\n                EleCount = stk.top();\n                stk.pop();\n                for (auto& p : temp) {\n                    EleCount[p.first] += p.second * count;\n                }\n            } else {\n                string element(1, formula[i++]);\n                while (i < n && islower(formula[i])) {\n                    element += formula[i++];\n                }\n                int count = 0;\n                while (i < n && isdigit(formula[i])) {\n                    count = count * 10 + (formula[i] - \'0\');\n                    i++;\n                }\n                if (count == 0)\n                    count = 1;\n                EleCount[element] += count;\n            }\n        }\n\n        return EleCount;\n    }\n\n    string FuncResult(unordered_map<string, int>& EleCount) {\n        vector<pair<string, int>> elements(EleCount.begin(),\n                                           EleCount.end());\n        sort(elements.begin(), elements.end());\n\n        string result;\n        for (auto& elem : elements) {\n            result += elem.first;\n            if (elem.second > 1) {\n                result += to_string(elem.second);\n            }\n        }\n        return result;\n    }\n\n    string countOfAtoms(string formula) {\n        unordered_map<string, int> EleCount = FuncCount(formula);\n        return FuncResult(EleCount);\n    }\n};\n
['Hash Table', 'String', 'Stack', 'Sorting', 'C++']