Java匹配两个字符串之间的每两个序列字符

时间:2018-08-05 09:45:06

标签: java string algorithm list for-loop

列表中的数值很少,因此我需要从左到右匹配每两个序列号/字符。

stringList下,我的记录很少,我必须将这些记录与另一个记录wordA进行比较。

我想匹配每两个序列号/字符,因此如果我输入630102,它必须返回第三个。如果我输入了630677,它必须返回第二个数字,因为630677中没有stringList,但是这里的第一个4数字6306与我的列表匹配值。

下面是我尝试的示例代码。如果您有任何建议,请提供。

String wordA = "630102"; 
List<String> stringList = new ArrayList<>();
stringList.add("630507");  //1st
stringList.add("630622");  //2nd
stringList.add("6301");    //3rd
stringList.add("63");      //4th
String common = "";

int count1 = 0;
int count2 = 0;
for (int i = 0; i < stringList.size(); i++) {
    for (int j = 0; j < wordA.length(); j++) {
        if (stringList.get(i).length() > j) {
            if (wordA.charAt(j) != stringList.get(i).charAt(j)) {
                break;
            } else if (wordA.charAt(j) == stringList.get(i).charAt(j)) {
                count1++;

                if (count1 == 2) {
                    count2++;
                    count1 = 0;
                }
            }
        }
    }
    if (wordA.length() > stringList.get(i).length()) {
        common = stringList.get(i);
        break;
    }
}
System.out.println("common is: " + common);

2 个答案:

答案 0 :(得分:2)

基本上有两种基本的解决方法:

    在列表和每个字符上
  1. 对于每个迭代

    您非常接近这一点。在这里,您去了:

    int maxLength = 0; 
    int indexOfLongestMatch = -1;
    for (int i = 0; i < stringList.size(); i++) {                  // Iterate the List
        String string =  stringList.get(i);
        int maxIndex = Math.min(wordA.length(), string.length());  // Compare lengths
        if (maxIndex >= maxLength) {                               // Worth to continue?
            int commonLength = 0;
            for (int j = 0; j < maxIndex; j++) {                   // Iterate characters
                if (string.charAt(j) == wordA.charAt(j)) {
                    commonLength++;                                // Any match counts
                    if (commonLength >= maxLength) {               // Check for the new max
                        maxLength = commonLength;                  // Register it
                        indexOfLongestMatch = i;                   // New index of the max
                    }
                }
            }
        }
    }
    

    stringList中最长匹配单词的索引为indexOfLongestMatch(根据您提供的相同输入等于2),单词为stringList.get(indexOfLongestMatch)({ {1}}。

  2. 正则表达式。正如@YCF_L在这里建议的那样,另一种可能的方式是Regex,它可以帮助您使用模式仅匹配具有常见初始字符的字符串:

    6301

    要通过输入实现此模式,必须将每个字符6?3?0?1?0?2? 替换为.,其中$0?是与点$0匹配的任何匹配字符。在Regex101

    上展示
    .

    然后,您只需在列表上进行一次迭代即可找到最长的匹配字符串及其索引:

    Pattern pattern = Pattern.compile(wordA.replaceAll(".", "$0?"));
    

    结果与上面的示例相同。 Matcher matcher; int indexOfLongestMatch = -1; int maxLength = 0; for (int i = 0; i < stringList.size(); i++) { // Iterate the List matcher = pattern.matcher(stringList.get(i)); // Apply the pattern if (matcher.matches()) { // Has a match? int length = matcher.group(0).length(); if (length >= maxLength) { // Check for the new max maxLength = length; // Register it indexOfLongestMatch = i; // New index of the max } } } 是找到的索引。


请注意,默认情况下,结果索引设置为indexOfLongestMatch,而不是列表中的现有索引-这意味着没有字符串以您想要的方式与输入匹配。

答案 1 :(得分:1)

据我了解,您想返回的值是单词中最常见的字符。这是我为您解决的问题:

    String wordA = "630677"; 
    List<String> stringList = new ArrayList<>();
    stringList.add("630507");  //1st
    stringList.add("630622");  //2nd
    stringList.add("6301");    //3rd
    stringList.add("63");      //4th
    String common = "";

    int commonCount = 0;
    int highestCount = 0;

    for(String s : stringList)
    {
        for(int i = 0; i < wordA.length(); i++) {
            if(s.length() > i && s.charAt(i) == wordA.charAt(i)) {
                commonCount++;
            }
            if(s.length() <= i || s.charAt(i) != wordA.charAt(i) || wordA.length() <= i + 1) {
                if(commonCount > highestCount) {
                    highestCount = commonCount;
                    common = s;
                }
                commonCount = 0;
                break;
            }

        }        }
    System.out.println(common.isEmpty() ? "There is no common." : "common is: " + common);
}

编辑:请注意,如果有更多具有相同数量正确字符串的字符串,它将始终从列表中返回第一个字符串。

相关问题