简体   繁体   English

两个字符串中长度为2(在相同索引处)的匹配子序列

[英]Matching subsequence of length 2 (at same index) in two strings

Given 2 strings, a and b, return the number of the positions where they contain the same length 2 substring. 给定2个字符串a和b,它们返回包含相同长度2子字符串的位置的数目。 For instance a and b is respectively "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. 例如,a和b分别为“ xxcaazz”和“ xxbaaz”的结果为3,这是因为“ xx”,“ aa”和“ az”子字符串在两个字符串中的位置相同。 What is wrong with my solution? 我的解决方案出了什么问题?

int count=0;
for(int i=0;i<a.length();i++)
{
   for(int u=i; u<b.length(); u++)
       {
        String aSub=a.substring(i,i+1);
        String bSub=b.substring(u,u+1);
        if(aSub.equals(bSub))
        count++;
    }
}
return count;
}

In order to fix your solution, you really don't need the inner loop. 为了解决您的解决方案,您实际上不需要内部循环。 Since the index should be same for the substrings in both string, only one loop is needed. 由于两个字符串中的子字符串的索引应相同,因此仅需要一个循环。

Also, you should iterate till 2nd last character of the smaller string, to avoid IndexOutOfBounds. 另外,您应该迭代直到较小字符串的倒数第二个字符,以避免IndexOutOfBounds。 And for substring , give i+2 as second argument instead. 对于substring ,请改用i+2作为第二个参数。

Overall, you would have to change your code to something like this: 总体而言,您将不得不将代码更改为以下内容:

int count=0;
for(int i=0; i < small(a, b).length()-1; i++)
{
        String aSub=a.substring(i,i+2);
        String bSub=b.substring(i,i+2);
        if(aSub.equals(bSub))
        count++;
    }
}
return count;

Why I asked about the length of string is, it might become expensive to create substrings of length 2 in loop. 为什么我问到字符串的长度是为什么,在循环中创建长度为2的子字符串可能会变得昂贵。 For length n of smaller string, you would be creating 2 * n substrings. 对于较小字符串的长度n ,您将创建2 * n个子字符串。

I would rather not create substring, and just match character by character, while keeping track of whether previous character matched or not. 我宁愿不创建子字符串,而只是逐个字符地匹配,同时跟踪先前的字符是否匹配。 This will work perfectly fine in your case, as length of substring to match is 2 . 这将在您的情况下完美地工作,因为要匹配的子字符串的长度为2 Code would be like: 代码如下:

String a = "iaxxai";
String b = "aaxxaaxx";

boolean lastCharacterMatch = false;
int count = 0;

for (int i = 0; i < Math.min(a.length(), b.length()); i++) {
  if (a.charAt(i) == b.charAt(i)) {
    if (lastCharacterMatch) {
      count++;  
    } else {
      lastCharacterMatch = true;
    }
  } else {
    lastCharacterMatch = false;
  }
}

System.out.println(count);

The heart of the problem lies with your usage of the substring method. 问题的核心在于您对substring方法的使用。 The important thing to note is that the beginning index is inclusive, and the end index is exclusive. 要注意的重要一点是,开始索引是包含的,结束索引是排他的。

As an example, dissecting your usage, String aSub=a.substring(i,i+1); 例如,剖析您的用法, String aSub=a.substring(i,i+1); in the first iteration of the loop i = 0 so this line is then String aSub=a.substring(0,1); 在循环的第一次迭代中,i = 0,因此此行是String aSub=a.substring(0,1); From the javadocs, and my explanation above, this would result in a substring from the first character to the first character or String aSub="x"; 从javadocs以及我上面的解释,这将导致从第一个字符到第一个字符的子字符串或String aSub="x"; Changing this to i+2 and u+2 will get you the desired behavior but beware of index out of bounds errors with the way your loops are currently written. 将其更改为i+2u+2将获得所需的行为,但是请注意当前编写循环的方式的索引超出范围错误。

 String a = "xxcaazz";
        String b = "xxbaaz";
        int count = 0;

        for (int i = 0; i < (a.length() > b.length() ? b : a).length() - 1; i++) {
            String aSub = a.substring(i, i + 2);
            String bSub = b.substring(i, i + 2);
            if (aSub.equals(bSub)) {
                count++;
            }
        }
        System.out.println(count);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取子序列匹配中的索引列表 - Get the list of index in subsequence matching 长度不同的两个字符串可以具有相同的哈希码吗? - Can two strings of different length have the same hashcode? 同一对字符串可能有不同的最长公共子序列吗? - Possible to have different Longest Common Subsequence for same pair of Strings? 将字符串除以长度为2 - Divide strings by length of two 最长递增子序列的长度 - Length of Longest increasing subsequence 最大连续子序列,返回子序列和长度 - Largest consecutive subsequence, return subsequence and length 创建两个相同长度的字符串,一个重复另一个结构,同时反复循环相同的字母 - Create two strings of same length, one duplicating the other's structure while looping the same letters over and over Java:将ArrayList字符串匹配到迭代器,并在同一索引处递增另一个ArrayList的整数 - Java: matching ArrayList strings to an iterator, and incrementing the integers of a different ArrayList at the same index 使用并行流从两个字符串中查找匹配字符的第一个索引 - Find first index of matching character from two strings using parallel streams 将两个列表合并到一个新列表中,索引到同一索引,但不增加长度 - Merging two lists into a new list, index to same index, while not adding to length
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM