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

Java来匹配两个字符串之间的每两个序列字符,java,string,algorithm,list,for-loop,Java,String,Algorithm,List,For Loop,我的列表中几乎没有数字值,所以我需要从左到右匹配每两个序列号/字符 在下面的stringList中,我几乎没有记录,我必须将这些记录与另一个wordA进行比较 我想匹配每两个序列号/字符,因此如果我输入630102它必须返回第三个序列号/字符。如果我输入了630677,它必须返回第二个,因为630677在我的stringList中不存在,但是这里第一个4数字6306与我的列表值匹配 下面是我尝试的示例代码。如果您有任何建议,请提供 String wordA = "630102"; List&l

我的列表中几乎没有数字值,所以我需要从左到右匹配每两个序列号/字符

在下面的
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);
String wordA=“630102”;
List stringList=新建ArrayList();
添加(“630507”)//第一
stringList.添加(“630622”)//第二
stringList.添加(“6301”)//第三
添加(“63”)//第四
字符串common=“”;
int count1=0;
int count2=0;
对于(int i=0;ij){
if(wordA.charAt(j)!=stringList.get(i.charAt(j)){
打破
}else if(wordA.charAt(j)=stringList.get(i.charAt(j)){
count1++;
如果(count1==2){
count2++;
count1=0;
}
}
}
}
if(wordA.length()>stringList.get(i.length()){
common=stringList.get(i);
打破
}
}
System.out.println(“common is:+common”);

据我所知,您希望返回一个值,该值包含单词中最常见的字符。这是我对您问题的解决方案:

    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);
}
String wordA=“630677”;
List stringList=新建ArrayList();
添加(“630507”)//第一
stringList.添加(“630622”)//第二
stringList.添加(“6301”)//第三
添加(“63”)//第四
字符串common=“”;
int commonCount=0;
int highestCount=0;
用于(字符串s:stringList)
{
for(int i=0;ii&&s.charAt(i)=wordA.charAt(i)){
commonCount++;
}

如果(s.length()基本上有两种基本方法可以解决这个问题:

  • 对于列表和每个字符的每次迭代

    你离这个很近。给你:

    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
                    }
                }
            }
        }
    }
    
    要从输入中实现此模式,必须将每个字符
    替换为
    $0?
    ,其中
    $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
            }
        }
    }
    
    Matcher-Matcher;
    int indexOfLongestMatch=-1;
    int maxLength=0;
    对于(inti=0;i=maxLength){//检查新的最大值
    maxLength=length;//注册它
    INDEXOFLONGESMATCH=i;//最大值的新索引
    }
    }
    }
    
    结果与上面的示例相同。
    indexOfLongestMatch
    是找到的索引



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

    为什么您希望它看起来很复杂?关于regex,它可以更容易些吗?@YCF\u L:regex?我发现使用regex的唯一方法是我的答案中的内容。就我个人而言,我更喜欢第一种方式-字符迭代。你有更好的主意吗?我想看看:))如果我输入631会怎么样?它不会返回635哦,你是对的!我更新了它,现在应该可以了!!如果我输入6310,它必须返回第四个63而不是第一个。是的,它应该这样做。它返回第一个值,因为它有两个公共字符,与最后一个相同,但正如下面的代码所述,它总是返回本例中的第一个字符串。如果这不是您想要的行为,请具体说明您想要实现的目标,因为我显然没有完全理解它…您对“特殊值”的含义是什么?您能给我举个例子吗?
    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
            }
        }
    }