BEATS 100 || STACK || EASY APPROACH
\n\n# Approach\n Describe your approach to solving the problem. \n\n#### 1. charToInt Function:\n- Converts a character digit (\'0\'-\'9\') to its corresponding
\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n#### 1. `charToInt` Function:\n- Converts a character digit (`\'0\'-\'9\'`) to its corresponding integer value (`0-9`). Used for converting numeric coefficients in the formula.\n\n#### 2. `isDigit` Function:\n- Checks if a character is a digit (`\'0\'-\'9\'`).\n\n#### 3. `countOfAtoms` Function:\n- **Stack and Maps Setup**: Uses a stack of `unordered_map<string, int>` to manage levels of nested parentheses. Each map stores atoms as keys and their counts as values.\n- **Parsing the Formula**:\n  - **Atoms**: Processes each atom by collecting uppercase letters (and optional following lowercase letters) and numeric coefficients.\n  - **Parentheses**: Handles opening `(` and closing `)` to manage nested structures. When encountering `(`, pushes the current `currentAtoms` map onto the stack and clears it for the new level. When encountering `)`, multiplies the atoms in `currentAtoms` by the following numeric coefficient and merges them with the atoms in the previous level from the stack.\n- **Sorting and Output**:\n  - After processing the entire formula, extracts atoms from `currentAtoms`, sorts them alphabetically, and constructs the result string by appending each atom followed by its count (if greater than 1).\n\n#### Example Execution with `"K4(ON(SO3)2)2"`:\n\n1. **Initial Setup**: Start with an empty stack and `currentAtoms` map.\n  \n2. **Processing Characters**:\n   - `K`: Add `"K"` with count `4` to `currentAtoms`.\n   - `(`: Push `currentAtoms` (now containing `"K"` with count `4`) onto the stack and clear `currentAtoms`.\n   - `O`: Add `"O"` to `currentAtoms`.\n   - `N`: Add `"N"` to `currentAtoms`.\n   - `(`: Push `currentAtoms` (containing `"O"` and `"N"`) onto the stack and clear `currentAtoms`.\n   - `S`: Add `"S"` to `currentAtoms`.\n   - `O`: Add `"O"` to `currentAtoms`.\n   - `3`: Multiply `"O"` in `currentAtoms` by `2`.\n   - `)`: Merge `currentAtoms` (now containing `"S"` and `"O"` with counts multiplied) with the top of the stack (containing `"O"` and `"N"`).\n   - `2`: Multiply `currentAtoms` (now merged with counts) by `2`.\n   - `)`: Merge `currentAtoms` (now containing counts multiplied) with the top of the stack (containing `"K"` with count `4`).\n   \n3. **Sorting and Output**:\n   - Extract atoms from `currentAtoms` and sort them (`"K"`, `"N"`, `"O"`, `"S"`).\n   - Construct the result string `"K4N2O14S2"`.\n\n4. **Result**: Output `"K4N2O14S2"`.\n\n# Code\n```\nclass Solution {\npublic:\n     int charToInt(char c) {\n        return c - \'0\';\n    }\n\n    string countOfAtoms(string formula) {\n        stack<unordered_map<string, int>> indicesStack; // Stack to store maps of atoms and their counts\n\n        unordered_map<string, int> currentAtoms; // Map to store current atom counts\n\n        int i = 0;\n        while (i < formula.length()) {\n            if (isupper(formula[i])) {\n                string atom;\n                atom += formula[i];\n                ++i;\n                while (i < formula.length() && islower(formula[i])) {\n                    atom += formula[i];\n                    ++i;\n                }\n                int count = 0;\n                while (i < formula.length() && isdigit(formula[i])) {\n                    count = count * 10 + charToInt(formula[i]);\n                    ++i;\n                }\n                count = max(1, count); // If no digit follows, count defaults to 1\n                currentAtoms[atom] += count;\n            } else if (formula[i] == \'(\') {\n                indicesStack.push(currentAtoms); // Save current atom counts to stack\n                currentAtoms.clear(); // Reset for atoms inside parentheses\n                ++i;\n            } else if (formula[i] == \')\') {\n                int count = 0;\n                ++i;\n                while (i < formula.length() && isdigit(formula[i])) {\n                    count = count * 10 + charToInt(formula[i]);\n                    ++i;\n                }\n                count = max(1, count); // If no digit follows, count defaults to 1\n\n                // Multiply atom counts inside parentheses by \'count\'\n                for (auto& entry : currentAtoms) {\n                    entry.second *= count;\n                }\n\n                if (!indicesStack.empty()) {\n                    // Merge current atoms into previous level (from stack)\n                    auto& prevAtoms = indicesStack.top();\n                    for (const auto& entry : currentAtoms) {\n                        prevAtoms[entry.first] += entry.second;\n                    }\n                    currentAtoms = prevAtoms; // Restore current atoms to merged state\n                    indicesStack.pop(); // Remove processed level from stack\n                }\n            } else {\n                ++i;\n            }\n        }\n\n        // Construct the result string\n        string ans;\n        vector<string> atoms;\n        for (auto& entry : currentAtoms) {\n            atoms.push_back(entry.first);\n        }\n        sort(atoms.begin(), atoms.end());\n\n        for (const string& atom : atoms) {\n            ans += atom;\n            if (currentAtoms[atom] > 1) {\n                ans += to_string(currentAtoms[atom]);\n            }\n        }\n\n        return ans;\n    }\n};\n```
['Hash Table', 'String', 'Stack', 'Sorting', 'C++']