[英]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+2
和u+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.