JAVA | Heavily Commented | Easy | Map | StringBuilder | 66% faster runtime
Intuition\n1. Solving the inner most bracket first using stacks.\n2. Using StringBuilder to perform operations on String is efficient (Using String: 22% faster 
# Intuition\n1. Solving the inner most bracket first using stacks.\n2. Using StringBuilder to perform operations on String is efficient (Using String: 22% faster runtime , Using StringBuilder: 66% faster runtime)\n3. Using Map to remove the repeated symbols.\n\n\n# Code\n```\nclass Solution {\n    public String countOfAtoms(String formula) {\n        //stores -> pair(symbol, count)\n        Stack<Pair<String, Integer>> solve = new Stack<>();\n        int len = formula.length();\n\n\n        for(int i = 0; i < len;) {\n\n            //if character is open bracket "("\n            if(formula.charAt(i) == \'(\') {\n                solve.push(new Pair<String, Integer>("(", 0));\n                i++;\n\n            } else if(formula.charAt(i) == \')\') {\n                //here we are solving for when we encounter ")" close bracket\n                //1. store the integer after ")" as we will have to multiply this number with the count of symbols\'s count inside "(X2Y3)5"\n                i++;\n                StringBuilder sec = new StringBuilder("0");\n                while(i<len && Character.isDigit(formula.charAt(i))) {\n                    sec.append(formula.charAt(i));\n                    i++;\n                }\n                Integer inte = Integer.parseInt(sec.toString());\n                inte = inte == 0 ? 1 : inte;\n\n                //popping elements until we encounter open brack -> now stack will contain element within the brackets "(X2Y3)"\n                Stack<Pair<String, Integer>> stack = new Stack<>();\n                while( !(solve.peek().getKey()).equals("(")) {\n                    stack.push(solve.pop());\n                }\n                //popping "(" open bracket from "solution"\n                solve.pop();\n\n                //popping stored element from stack and adding them to store by multiplying the count "inte" Ex: (X2Y3)5 -> inte=5  stack = (X,2), (Y,3)\n                //storing in "solve" -> (X,2*5), (Y,3*5)\n                while(!stack.isEmpty()) {\n                    Pair<String, Integer> p = stack.pop();\n                    solve.push(new Pair<String, Integer>(p.getKey(), p.getValue()*inte));\n                }\n\n            } else if(Character.isUpperCase(formula.charAt(i))) {\n                //encounterd a upper case element that will be stores in solve as a particular element\n                //"sym" stores symbl name\n                StringBuilder sym = new StringBuilder(formula.charAt(i)+"");\n                i++;\n                while(i < len && Character.isLowerCase(formula.charAt(i))) {\n                    sym.append(formula.charAt(i)+"");\n                    i++;\n                }\n\n                //"sec" stores string form integer count\n                StringBuilder sec = new StringBuilder("0");\n                while(i < len && Character.isDigit(formula.charAt(i))) {\n                    sec.append(formula.charAt(i));\n                    i++;\n                }\n                Integer inte = Integer.parseInt(sec.toString());\n                inte = inte == 0 ? 1 : inte;\n                solve.push(new Pair<String, Integer>(sym.toString(), inte));\n            }\n        }\n\n\n        //combining the repeated elements using map\n        //Ex -> (O2X2O5) -> (O7X2)\n        Map<String, Integer> map = new HashMap<>();\n        Stack<String> dummy = new Stack<>();\n        while(!solve.isEmpty()) {\n            Pair<String, Integer> p = solve.pop();\n            if(!map.containsKey(p.getKey())) dummy.add(p.getKey());\n            map.put(p.getKey(), map.getOrDefault(p.getKey(), 0)+p.getValue());\n        }\n\n        //sorting according to the names of unique elements\n        Collections.sort(dummy, Collections.reverseOrder());\n\n        //storing concatenated answer in StringBuilder ans\n        StringBuilder ans = new StringBuilder();\n        while(!dummy.isEmpty()) {\n            String s = dummy.pop();\n            ans.append(s);\n            if(map.get(s) != 1) ans.append(map.get(s)+"");\n        }\n\n        return ans.toString();\n    }\n}\n```
['Hash Table', 'String', 'Stack', 'Java']