class MolecularFormulaManipulator {

    public static String simplifyMolecularFormula(String formula) {
        String newFormula = formula;
        char thisChar;

        if (formula.contains(" ")) {
            newFormula = newFormula.replace(" ", "");
        }
        if (!formula.contains(".")) return breakExtractor(formula);

        List<String> listMF = new ArrayList<String>();
        while (newFormula.contains(".")) {
            int pos = newFormula.indexOf('.');
            String thisFormula = newFormula.substring(0, pos);
            if (thisFormula.charAt(0) >= '0' && thisFormula.charAt(0) <= '9')
                thisFormula = multipleExtractor(thisFormula);

            if (thisFormula.contains("(")) thisFormula = breakExtractor(thisFormula);

            listMF.add(thisFormula);
            thisFormula = newFormula.substring(pos + 1, newFormula.length());
            if (!thisFormula.contains(".")) {

                if (thisFormula.charAt(0) >= '0' && thisFormula.charAt(0) <= '9')
                    thisFormula = multipleExtractor(thisFormula);

                if (thisFormula.contains("(")) thisFormula = breakExtractor(thisFormula);

                listMF.add(thisFormula);
            }
            newFormula = thisFormula;
        }
        if (newFormula.contains("(")) newFormula = breakExtractor(newFormula);

        String recentElementSymbol = "";
        String recentElementCountString = "0";

        List<String> eleSymb = new ArrayList<String>();
        List<Integer> eleCount = new ArrayList<Integer>();
        for (int i = 0; i < listMF.size(); i++) {
            String thisFormula = listMF.get(i);
            for (int f = 0; f < thisFormula.length(); f++) {
                thisChar = thisFormula.charAt(f);
                if (f < thisFormula.length()) {
                    if (thisChar >= 'A' && thisChar <= 'Z') {
                        recentElementSymbol = String.valueOf(thisChar);
                        recentElementCountString = "0";
                    }
                    if (thisChar >= 'a' && thisChar <= 'z') {
                        recentElementSymbol += thisChar;
                    }
                    if (thisChar >= '0' && thisChar <= '9') {
                        recentElementCountString += thisChar;
                    }
                }
                if (f == thisFormula.length() - 1
                        || (thisFormula.charAt(f + 1) >= 'A' && thisFormula.charAt(f + 1) <= 'Z')) {
                    int posit = eleSymb.indexOf(recentElementSymbol);
                    int count = Integer.valueOf(recentElementCountString);
                    if (posit == -1) {
                        eleSymb.add(recentElementSymbol);
                        eleCount.add(count);
                    } else {
                        int countP = Integer.valueOf(recentElementCountString);
                        if (countP == 0) countP = 1;
                        int countA = eleCount.get(posit);
                        if (countA == 0) countA = 1;
                        int value = countP + countA;
                        eleCount.remove(posit);
                        eleCount.add(posit, value);
                    }

                }
            }
        }
        String newF = "";
        for (int i = 0; i < eleCount.size(); i++) {
            String element = eleSymb.get(i);
            int num = eleCount.get(i);
            if (num == 0)
                newF += element;
            else
                newF += element + num;

        }
        return newF;
    }

}