Java 给定两个字符串a和b,返回它们包含相同长度2子字符串的位置数

Java 给定两个字符串a和b,返回它们包含相同长度2子字符串的位置数,java,Java,这是我回答上述问题的代码。我很想知道为什么代码不能在所有情况下工作,例如: System.out.println(stringMatch("aabbccdd", "abbbxxd")); // Should be 1, but was 3 System.out.println(stringMatch("aaxxaaxx", "iaxxai")); // Should be 3, but was 5 System.out.println(stringMatch("iaxxai", "aaxxaax

这是我回答上述问题的代码。我很想知道为什么代码不能在所有情况下工作,例如:

System.out.println(stringMatch("aabbccdd", "abbbxxd")); // Should be 1, but was 3
System.out.println(stringMatch("aaxxaaxx", "iaxxai")); // Should be 3, but was 5
System.out.println(stringMatch("iaxxai", "aaxxaaxx")); // Should be 3, but was 5
public int stringMatch(字符串a、字符串b){
int计数器=0;
对于(int i=0;i
一个错误是
<?.length()-2
由于迭代是针对
i+2
的,正如Ashu所提到的,您的程序实际上是在计算长度等于2的字符串的数量

以你提到的第一个例子为例。。 aabbccdd,abbbxxd

因此,for循环的结构方式是将“aa”子字符串与第二个字符串“abbbxxd”中的每个两个字母的子字符串进行比较

最后你有ab和ab的匹配,bb和bb的匹配,bb和bb的匹配,
(因为在第二个字符串中有3个b),因此得到3作为输出,而实际上它应该是1,因为只有一个bb匹配到相同的位置(第三个和第四个字母)

问题要求您将两个字符串放在一起,并检查它们有多少个位置具有相同的两个字符集(注意它们可能会超高)。换句话说:

aabbccdd        aaxxaaxx         iaxxai
abbbxxd         iaxxai           aaxxaaxx
--^^---- (1)    -^^^^---  (3)    -^^^^---  (3)
 (bb)            (ax, xx, xa)      (ax, xx, xa)
正如您希望看到的那样,您只需要一个循环就可以完成此操作,因为您总是比较字符串a和字符串b的相同索引。但是您的代码有两个循环。它所做的是检查字符串a的任意两个字符是否在字符串b中的任何位置:

   for string "aabbccdd":
Iteration i1:  Iteration i2:  Iteration i3:  Iteration i4: etc...
abbbxxd        abbbxxd        abbbxxd        abbbxxd
aa (0)         ab (1)          bbb (2)       bc (0)
您需要去掉第一个循环,然后修复循环,使其运行到最短字符串结尾前的2,这样您就不会得到ArrayIndexOutofBounds异常:

public int stringMatch(String a, String b) {
    int counter = 0;

    for(int i = 0; ((i < a.length() - 2) && (i < b.length() -2)); i++){

        String aSub = a.substring(i, i + 2);
        String bSub = b.substring(i, i + 2);

        if(aSub.equals(bSub)){
        counter++;
        }
    }
return counter;
}
public int stringMatch(字符串a、字符串b){
int计数器=0;
对于(int i=0;((i
感谢您在我的for语句中发现错误!这两个语句都必须为true,循环才能继续运行,而不是两者都为true

 public static int stringMatch(String a, String b) {
    int counter = 0;

    for (int i = 0; i < a.length() - 1; i++) {

        for (int j = 0; j < b.length() - 1; j++) {
            String aSub = a.substring(i, i + 2);
            String bSub = b.substring(j, j + 2);

            if (aSub.equals(bSub)) {
                System.out.println(aSub);
                counter++;
            }
        }
    }
    return counter;
}

public static void main(String[] args) {

    System.out.print(stringMatch("aabbccdd", "abbbxxd"));
    System.out.println("****");
    System.out.println(stringMatch("aaxxaaxx", "iaxxai"));
    System.out.println("****");
    System.out.println(stringMatch("iaxxai", "aaxxaaxx"));

}

这反过来又是符合您的代码的正确输出。如果您想要匹配的子字符串的唯一计数,可以尝试在集合中插入子字符串,并返回集合的大小作为函数的输出。

首先,您应该找到更大的字符串:

String tempA = "";
String tempB = "";
if (a.length() > b.length()){
    tempA = a;
    tempB = b;
}else{
    tempA = b;
    tempB = a;
} 
然后您需要控制一些特殊情况,例如当i等于
aTemp.length()-1
或j等于
bTemp.length()-1
时。在这种情况下
aTemp.substring(i,i+2);
bTemp.substring(j,j+2);
语句发生错误

我应该迭代到aTemp.length()-2

j应该迭代到bTemp.length()-2

您需要存储查找到的位置以避免重复计数(我是通过使用ArrayList完成的)

对于本例:stringMatch(“iaxxai”、“aaxxaaxx”) 答案必须是3:

ax:iaxxai aaxxaaxx

xx:iaxxai aaxxaaxx

xa:iaxxaiaxxaaxx

public static int stringMatch(String a, String b) {
  ArrayList<String> list = new ArrayList<String>();
  int counter = 0;
  String tempA = "";
  String tempB = "";
  if (a.length() > b.length()){//to find the bigger string
      tempA = a;
      tempB = b;
  }else{
      tempA = b;
      tempB = a;
  } 
  for(int i = 0; i < tempA.length() - 2; i++){

    for(int j = 0; j < tempB.length() - 2; j++){
        String aSub;
        String bSub;
        if (i == tempA.length() - 2){//when i == tempA.length() - 2 you need to take the substring(i to end of the string) 
             aSub = tempA.substring(i);
        }else{
            aSub = tempA.substring(i, i + 2);
        }
        if (j == tempB.length() - 2){
             bSub = tempB.substring(j);
        }else{
            bSub = tempB.substring(j, j + 2);
        }
        if(aSub.equals(bSub) && list.indexOf(aSub) == -1 ){//list.indexOf(aSub) == -1 means there is no element in the list
            list.add(aSub);//or bSub
            counter++;
        }
    }
  }
  return counter;
公共静态int-stringMatch(字符串a、字符串b){
ArrayList=新建ArrayList();
int计数器=0;
字符串tempA=“”;
字符串tempB=“”;
如果(a.length()>b.length()){//查找较大的字符串
tempA=a;
tempB=b;
}否则{
tempA=b;
tempB=a;
} 
对于(int i=0;i

}

您预期的输出不是很清楚。您的程序实际上正在计算长度为2的子字符串的数量。stringMatch(“aabbccdd”,“abbbxxd”)=ab,bb,bb(计数为3)stringMatch(“aaxxaaxx”,“iaxxai”)=ax,xx,xa,ax,xx(计数为5)stringMatch(“iaxxai”,“aaxxaaxx”)=ax,ax,xx,xx(计数为5)如果这不是预期的输出,您的要求是什么?一个
int
如何能容纳
13x
?这是不清楚的。@WanderiKabithi-如上所述,为了得到满意的答案,请进一步解释这一点。例如,对于第一个
字符串,我认为3是合理的,第二个
字符串有
aa
和2个不同的
bb
s。这是否与OPs代码相同,他们声称OPs代码给出了不正确的输出?如果目的是在任何位置找到匹配的子字符串,则输出是正确的。
String tempA = "";
String tempB = "";
if (a.length() > b.length()){
    tempA = a;
    tempB = b;
}else{
    tempA = b;
    tempB = a;
} 
public static int stringMatch(String a, String b) {
  ArrayList<String> list = new ArrayList<String>();
  int counter = 0;
  String tempA = "";
  String tempB = "";
  if (a.length() > b.length()){//to find the bigger string
      tempA = a;
      tempB = b;
  }else{
      tempA = b;
      tempB = a;
  } 
  for(int i = 0; i < tempA.length() - 2; i++){

    for(int j = 0; j < tempB.length() - 2; j++){
        String aSub;
        String bSub;
        if (i == tempA.length() - 2){//when i == tempA.length() - 2 you need to take the substring(i to end of the string) 
             aSub = tempA.substring(i);
        }else{
            aSub = tempA.substring(i, i + 2);
        }
        if (j == tempB.length() - 2){
             bSub = tempB.substring(j);
        }else{
            bSub = tempB.substring(j, j + 2);
        }
        if(aSub.equals(bSub) && list.indexOf(aSub) == -1 ){//list.indexOf(aSub) == -1 means there is no element in the list
            list.add(aSub);//or bSub
            counter++;
        }
    }
  }
  return counter;