如何查找连续重复的未知子字符串

Jak*_*ger 5 java string substring

我正在尝试建立一个系统来分解(如果该术语在化学中是正确的)一个给定的扩展化学公式,例如C6H2NO2NO2NO2CH3括号,以便它C?H?(NO?)?CH?.(在第一个实例中忽略下标或缺少下标).问题是,我不知道重复的分子会是什么,甚至不知道它会持续多久.我如何找到并计算重复次数?

对于上下文,这是我到目前为止的代码,它从2D元素列表生成公式:

private String getFormula(List<List<Element>> elements)
{
    String formula = ""; //TODO Switch out for StringBuilder

    for(List<Element> currentElement : elements)
    {
        formula += currentElement.get(0).getSymbol(); //Every element per list is identical, so looking at 0 will always be safe
        if(currentElement.size() > 1) formula += currentElement.size(); //Only display a number if there is more than 1 element
    }

    return formula;
}
Run Code Online (Sandbox Code Playgroud)

rob*_*los 2

编辑更新以便考虑顺序。谢谢@JakeStanger!
第二次编辑更新以反映分子以 结尾的新条件,|


我使用正则表达式进行分割|,因为从给定的String我们知道一个新分子在 后开始|。我用 aHashmap来记录每种类型的分子数量。最后,我迭代了 中的每个值并附Hashmap加到取决于String result它是否是一个分子。干杯!

   public static String factorise(String input) {
        String result = "";
        Map<String, Integer> molecules = new LinkedHashMap<>();
        String[] res = input.split("\\|");
        for (String t : res) {
            //Check if we already have this element in our map
            if (!molecules.containsKey(t)) {
                //If not then add it and set the count to 1
                molecules.put(t, 1);
            } else {
                //If we do then update the count by 1
                molecules.put(t, molecules.get(t) + 1);
            }
        }
        //Iterate through each molecule
        for (String key : molecules.keySet()) {
            if (molecules.get(key) == 1) {
                //If the count is only at one, then we just need to append it.
                result += key;
            } else {
                //Otherwise, we need the parentheces and the number of repetitions followed after
                result = result + "(" + key + ")" + molecules.get(key);
            }
        }
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

跑步

    System.out.println(factorise("C6|H2|NO2|NO2|NO2|CH3|OH|OH"));
    System.out.println(factorise("HO|HO"));
Run Code Online (Sandbox Code Playgroud)

运行时产生以下结果:

运行:
C6H2(NO2)3CH3(OH)2
(H2O)2
构建成功(总时间:0 秒)