Beginner Solution with Stack and Hashmap (P.S. Somehow My Submission Beats Time Complexity 100% 😊)
Intuition\nThe code aims to count the number of atoms in a chemical formula. It parses the formula, keeps track of the atoms and their counts, and then returns 
# Intuition\nThe code aims to count the number of atoms in a chemical formula. It parses the formula, keeps track of the atoms and their counts, and then returns a string with the atoms and their counts in a specific format.\n\n# Approach\n1. Iterate through the formula: Loop through each character in the formula.\n2. Identify atoms:\n   - If the character is a capital letter, it\'s the start of an atom symbol.\n   - If the character is lowercase, it\'s part of the same atom symbol.\n   - If the character is a digit, it indicates the count of the atom.\n3. Handle parentheses:\n   - When encountering an opening parenthesis, push it onto a stack.\n   - When encountering a closing parenthesis, multiply the count of atoms within the parentheses by the multiplier outside.\n4. Store counts: Maintain a hashmap to store the counts of each atom.\n5. Sort and format: Sort the atoms alphabetically and create a string with the atom symbols and their counts.\n\n## Visualization:\n- Formula: "K4(ON(SO3)2)2"\n- Stack:\n[ \'K\', 4, \'\', \'O\', 2, \'N\', 2, \'\', \'S\', 4, \'O\', 12]\n- Hashmap:\n{ K: 4, O: 14, N: 2, S: 4 }\n\n# Complexity\n- Time complexity: O(n log n)\n\n- Space complexity: O(n)\n\n# Code\n```\n/**\n * @param {string} formula\n * @return {string}\n */\nvar countOfAtoms = function (formula) {\n  const hashmap = {};\n  const stack = [];\n\n  for (let i = 0; i < formula.length; i++) {\n    let char = formula[i];\n    let nextChar = formula[i + 1];\n\n    // alpha\n    if (char >= "A" && char <= "Z") {\n      if (nextChar >= "a" && nextChar <= "z") {\n        stack.push(char + nextChar);\n        i++;\n      } else {\n        stack.push(char);\n      }\n\n      if (formula[i + 1] < "1" || formula[i + 1] > "9") {\n        stack.push(1);\n      }\n    }\n\n    // numeric\n    if (char >= "1" && char <= "9") {\n      let digits = char;\n      while (nextChar >= "0" && nextChar <= "9") {\n        digits += nextChar;\n        i++;\n        nextChar = formula[i + 1];\n      }\n\n      stack.push(+digits);\n    }\n\n    // open parentheses\n    if (char === "(") {\n      stack.push(char);\n    }\n\n    // close parentheses\n    if (char === ")") {\n      let multiply = "";\n\n      if (nextChar >= "1" && nextChar <= "9") {\n        multiply += nextChar;\n        i++;\n        nextChar = formula[i + 1];\n        while (nextChar >= "0" && nextChar <= "9") {\n          multiply += nextChar;\n          i++;\n          nextChar = formula[i + 1];\n        }\n      }\n\n      if (multiply === "") {\n        multiply = 1;\n      }\n\n      multiply = +multiply;\n\n      // find open parantheses\n      let pointer = stack.length - 1;\n      while (true) {\n        if (typeof stack[pointer] === "number") {\n          stack[pointer] = stack[pointer] * multiply;\n        }\n\n        if (stack[pointer] === "(") {\n          stack[pointer] = "";\n          break;\n        }\n\n        pointer--;\n      }\n    }\n  }\n\n  // loop through the stack and insert it into hashmap\n  for (let i = 0; i < stack.length; i++) {\n    if (stack[i] === "") {\n      continue;\n    }\n\n    if (typeof stack[i] === "string") {\n      const symbol = stack[i];\n      const count = !stack[i + 1] ? 1 : stack[i + 1];\n\n      if (hashmap[symbol]) {\n        hashmap[symbol] += count;\n      } else {\n        hashmap[symbol] = count;\n      }\n    }\n  }\n\n  const res = Object.keys(hashmap)\n    .sort()\n    .reduce((str, key) => {\n      str += `${key}${hashmap[key] === 1 ? "" : hashmap[key]}`;\n      return str;\n    }, "");\n\n  return res;\n};\n```
['JavaScript']