有人可以告诉我,在试图根据给定的查询找到给定字符串中的子字符串计数时,我在Java代码中做错了什么?

发布于 2025-02-08 05:21:18 字数 1509 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2025-02-15 05:21:18
  • 您应该制作count第一个循环本地变量,以使每个后缀的计数从0开始。

  • 使用 字符串#endswith 检查字符串是否以特定后缀结束,并每次将计数增加1时。

for (int i = 0; i < findStr.length; i++) {
    int count = 0;
    for(String str: strr)
        if(str.endsWith(findStr[i])) ++count;
    result[i] = count;
}
for(int l: result)
    System.out.println(l);
  • 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.

for (int i = 0; i < findStr.length; i++) {
    int count = 0;
    for(String str: strr)
        if(str.endsWith(findStr[i])) ++count;
    result[i] = count;
}
for(int l: result)
    System.out.println(l);
唐婉 2025-02-15 05:21:18

您需要在每次迭代开始时重置计数到0。
否则,它也将计算上一个以前的子字符串的发生。

for (int i = 0; i < findStr.length; i++) {
    count = 0;
    for (int j = 0; j < strr.length; j++) {
        count += findCount(strr[j], findStr[i]);
    }
    result[i] = count;
}

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.

for (int i = 0; i < findStr.length; i++) {
    count = 0;
    for (int j = 0; j < strr.length; j++) {
        count += findCount(strr[j], findStr[i]);
    }
    result[i] = count;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文