【问题标题】:Java to match every two sequence characters between two stringJava匹配两个字符串之间的每两个序列字符
【发布时间】:2019-01-12 13:28:58
【问题描述】:

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

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);

【问题讨论】:

  • 你为什么希望它看起来很复杂??
  • 正则表达式怎么样,它可以更容易吗?
  • @YCF_L:正则表达式?我发现使用正则表达式的唯一方法就是我的答案。就个人而言,我更喜欢第一种方式——按字符迭代。你有更好的主意吗?我想看看:))

标签: java string algorithm list for-loop


【解决方案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);
}

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

【讨论】:

  • 如果我输入 631 会怎样?它不会返回 63
  • 哦,你是对的!我更新了,现在应该可以用了!!
  • 再次,如果我输入 6310 这是错误的,它必须返回第四个 63 而不是第一个。
  • 是的,它应该这样做。它返回第一个值,因为它有两个公共字符,与最后一个相同,但如下代码所述,在这种情况下它总是返回第一个字符串。如果这不是您想要的行为,请具体说明您想要实现的目标,因为我显然没有完全理解它......
【解决方案2】:

关于如何解决这个问题,基本上有两种基本方法:

  1. For-each 迭代在列表和每个字符上

    你和这个很接近。给你:

    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) (6301)。

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

    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,这不是列表中的现有索引 - 这意味着没有任何字符串以您想要的方式与输入匹配。

【讨论】:

  • “特定价值”是什么意思?你能给我举个例子吗?
猜你喜欢
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
相关资源
最近更新 更多