Java Simple Solution
\nclass Solution {\n    public String countOfAtoms(String formula) {\n        Deque<Map> m = new LinkedList<Map>();\n        Map<String, Integer> x = new TreeMa
```\nclass Solution {\n    public String countOfAtoms(String formula) {\n        Deque<Map> m = new LinkedList<Map>();\n        Map<String, Integer> x = new TreeMap<>();\n        m.offerLast(x);\n        int i = 0;\n        while(i < formula.length()){\n            //System.out.println(x.toString());\n            if(formula.charAt(i) == \'(\'){\n                x = new HashMap<>();\n                m.offerLast(x);\n                i++;\n                continue;\n            }\n            if(formula.charAt(i) == \')\'){\n                m.pollLast();\n                int tmp = 0;\n                while((++i < formula.length()) && (Character.isDigit(formula.charAt(i))))\n                    tmp = tmp * 10 + formula.charAt(i) - \'0\';\n                Map<String, Integer> y = m.peekLast();\n                for(String key: x.keySet())\n                    y.put(key, y.getOrDefault(key, 0)+x.get(key)*tmp);\n                x = y;\n                continue;\n            }\n            if(Character.isLetter(formula.charAt(i))){\n                String s = Character.toString(formula.charAt(i)); //?\n                if((++i < formula.length())&&(Character.isLowerCase(formula.charAt(i))))\n                    s += formula.charAt(i++);\n                int tmp = 0;\n                i--;\n                while((++i < formula.length()) && (Character.isDigit(formula.charAt(i))))\n                    tmp = tmp * 10 + formula.charAt(i) - \'0\';\n                tmp = tmp>0?tmp:1;\n                x.put(s, x.getOrDefault(s,0)+tmp);\n            }\n        }\n        StringBuffer sb = new StringBuffer();\n        for(String key: x.keySet()){\n            sb.append(key);\n            if(x.get(key) != 1)\n                sb.append(x.get(key));\n        }\n        return sb.toString();\n    }\n}\n```
[]