C# Solution
Not a good code. But it works. Don't Down Vote Please!!\nThe idea is: if you have an open bracket update the counts and store it in a stack. Otherwise, update t
Not a good code. But it works. Don't Down Vote Please!!\nThe idea is: if you have an open bracket update the counts and store it in a stack. Otherwise, update the dictionary. There is redundant code, need to clean up.\n```\npublic string CountOfAtoms(string formula)\n        {\n            string result = string.Empty;\n            // "K4(ON(SO3)2)2"\n            int open_brackets = 0;\n            var d = new Dictionary<string, int>();\n            var name_stack = new Stack<string>();\n            var value_stack = new Stack<int>();\n\n            for (int i = 0; i < formula.Length; i++)\n            {\n                string atom = string.Empty;\n                //(H2O)2\n                if (formula[i] == '(')\n                {\n                    open_brackets++;\n                    name_stack.Push("(");\n                    value_stack.Push(-1);\n                }\n                else if (formula[i] == ')')\n                {\n                    open_brackets--;\n\n                    i++;\n                    while (i < formula.Length && char.IsDigit(formula[i]))\n                    {\n                        atom += formula[i];\n                        i++;\n                    }\n\n                    i--;\n                    int mul = atom.Length > 0 ? int.Parse(atom) : 1;\n\n                    if (open_brackets > 0)\n                    {\n                        var temp_name_stack = new Stack<string>();\n                        var temp_value_stack = new Stack<int>();\n\n                        while (name_stack.Count > 0 && name_stack.Peek() != "(")\n                        {\n                            temp_name_stack.Push(name_stack.Pop());\n                            temp_value_stack.Push(value_stack.Pop() * mul);\n                        }\n\n                        name_stack.Pop();\n                        value_stack.Pop();\n\n                        while (temp_name_stack.Count > 0)\n                        {\n                            name_stack.Push(temp_name_stack.Pop());\n                            value_stack.Push(temp_value_stack.Pop());\n                        }\n                    }\n                    else\n                    {\n                        while (name_stack.Count > 0 && name_stack.Peek() != "(")\n                        {\n                            if (d.ContainsKey(name_stack.Peek()))\n                            {\n                                d[name_stack.Pop()] += mul * value_stack.Pop();\n                            }\n                            else\n                            {\n                                d.Add(name_stack.Pop(), mul * value_stack.Pop());\n                            }\n                        }\n\n                        name_stack.Pop();\n                        value_stack.Pop();\n                    }\n                }\n\n\n                else if (char.IsLetter(formula[i]) && char.IsUpper(formula[i]))\n                {\n                    atom += formula[i];\n                    i++;\n\n                    while (i < formula.Length && char.IsLower(formula[i]))\n                    {\n                        atom += formula[i];\n                        i++;\n                    }\n\n                    string value = "";\n\n                    while (i < formula.Length && char.IsDigit(formula[i]))\n                    {\n                        value += formula[i];\n                        i++;\n                    }\n\n                    i--;\n                    int m = value.Length > 0 ? int.Parse(value) : 1;\n                    if (open_brackets > 0)\n                    {\n                        name_stack.Push(atom);\n                        value_stack.Push(m);\n                    }\n                    else\n                    {\n                        if (d.ContainsKey(atom))\n                        {\n                            d[atom] += m;\n                        }\n                        else\n                        {\n                            d.Add(atom, m);\n                        }\n                    }\n                }\n            }\n\n            while (name_stack.Count > 0)\n            {\n                if (d.ContainsKey(name_stack.Peek()))\n                {\n                    d[name_stack.Pop()] += value_stack.Pop();\n                }\n                else\n                {\n                    d.Add(name_stack.Pop(), value_stack.Pop());\n                }\n            }\n\n            d.OrderBy((x) => x.Key).ToList().ForEach(y =>\n            {\n                result += y.Key;\n                if (y.Value > 1)\n                {\n                    result += y.Value;\n                }\n            });\n\n            return result;\n        }\n\n```
[]