有人可以告诉我,在试图根据给定的查询找到给定字符串中的子字符串计数时,我在Java代码中做错了什么?
Array Findstr中都有查询,对于每个查询,我都会给我一个目标后缀。我必须确定具有后缀作为目标后缀的阵列strr中的字符串计数。查询作为字符串阵列Findstr给出。
示例
假设
strr = [“ asdfc”,“ asfc”,“ vdsfc”,“ trgfds”,“ egregds”,“ tertdfc”,“ rtyergds”]
findstr = [“ dfc”,“ dfc”,“ fc”,“ fc”,“ fc” ,“ DS”]
方法:
在第一个查询中,所需的后缀为“ DFC”。具有此后缀的字符串为[ASDFC,TertDFC]。因此,计数为2。
在第二查询中,所需的后缀为“ fc”。具有此后缀的字符串为[ASDFC,ASFC,VDSFC,TERTDFC]。因此,计数为4。
在第三查询中,所需的后缀为“ ds”。具有此后缀的字符串为[trgfds,egregds,rtyergds]。因此,计数为
。
3 6,10] 。谁能告诉我我在这里做错了什么?
class FindCount {
public static void main(String[] args) {
String[] strr = new String[]{"asdfc", "asfc", "vdsfc", "trgfds", "egregds", "tertdfc", "rtyergds"};
String[] findStr = new String[]{"dfc", "fc", "ds"};
int count = 0;
int result[] = new int[findStr.length];
for (int i = 0; i < findStr.length; i++) {
for (int j = 0; j < strr.length; j++) {
count += findCount(strr[j], findStr[i]);
}
result[i] = count;
}
for(int l: result)
System.out.println(l);
}
static int findCount(String str, String findStr) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += findStr.length();
}
}
return count;
}
}
There are queries in array findStr, and for each query, I am given a target suffix. I have to determine the count of strings in the array strr that have the suffix as the target suffix. The queries are given as an array of strings findStr.
Example
Assumptions
strr = ["asdfc", "asfc", "vdsfc", "trgfds", "egregds", "tertdfc", "rtyergds"]
findStr = ["dfc", "fc", "ds"]
Approach:
In the 1st Query, the required suffix is "dfc". The strings that have this suffix are [asdfc, tertdfc]. Hence, the count is 2.
In 2nd query, the required suffix is "fc". The strings that have this suffix are [asdfc, asfc, vdsfc, tertdfc]. Hence, the count is 4.
In 3rd query, the required suffix is "ds". The strings that have this suffix are [trgfds, egregds, rtyergds]. Hence, the count is 3.
Hence the output is [2,4,3].
But When I'm trying to do this using my code I'm getting wrong output [2,6,10]. Can anyone tell me what I'm doing wrong here ?
class FindCount {
public static void main(String[] args) {
String[] strr = new String[]{"asdfc", "asfc", "vdsfc", "trgfds", "egregds", "tertdfc", "rtyergds"};
String[] findStr = new String[]{"dfc", "fc", "ds"};
int count = 0;
int result[] = new int[findStr.length];
for (int i = 0; i < findStr.length; i++) {
for (int j = 0; j < strr.length; j++) {
count += findCount(strr[j], findStr[i]);
}
result[i] = count;
}
for(int l: result)
System.out.println(l);
}
static int findCount(String str, String findStr) {
int lastIndex = 0;
int count = 0;
while (lastIndex != -1) {
lastIndex = str.indexOf(findStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += findStr.length();
}
}
return count;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该制作
count
第一个循环本地变量,以使每个后缀的计数从0开始。使用
字符串#endswith
检查字符串是否以特定后缀结束,并每次将计数增加1时。You should make the
count
variable local to the first loop so that the count starts at 0 for each suffix.Use
String#endsWith
to check if a string ends with a particular suffix, and increment the count by 1 each time this is true.您需要在每次迭代开始时重置
计数
到0。否则,它也将计算上一个以前的子字符串的发生。
You need to reset
count
to 0 at the beginning of each iteration.Otherwise it will count the occurrences of the previous substrings as well.