content: class Solution {
      int i;
      public String countOfAtoms(String formula) {
          StringBuilder ans = new StringBuilder();
          i = 0;
          Map<String, Integer> count = parse(formula);
          for (String name: count.keySet()) {
              ans.append(name);
              int multiplicity = count.get(name);
              if (multiplicity > 1) ans.append("" + multiplicity);
          }
          return new String(ans);
      }
  
      public Map<String, Integer> parse(String formula) {
          int N = formula.length();
          Map<String, Integer> count = new TreeMap();
          while (i < N && formula.charAt(i) != ')') {
              if (formula.charAt(i) == '(') {
                  i++;
                  for (Map.Entry<String, Integer> entry: parse(formula).entrySet()) {
                      count.put(entry.getKey(), count.getOrDefault(entry.getKey(), 0) + entry.getValue());
                  }
              } else {
                  int iStart = i++;
                  while (i < N && Character.isLowerCase(formula.charAt(i))) i++;
                  String name = formula.substring(iStart, i);
                  iStart = i;
                  while (i < N && Character.isDigit(formula.charAt(i))) i++;
                  int multiplicity = iStart < i ? Integer.parseInt(formula.substring(iStart, i)) : 1;
                  count.put(name, count.getOrDefault(name, 0) + multiplicity);
              }
          }
          int iStart = ++i;
          while (i < N && Character.isDigit(formula.charAt(i))) i++;
          if (iStart < i) {
              int multiplicity = Integer.parseInt(formula.substring(iStart, i));
              for (String key: count.keySet()) {
                  count.put(key, count.get(key) * multiplicity);
              }
          }
          return count;
      }
  }