简体   繁体   中英

Can someone tell me what am I doing wrong here in my Java code when trying to find count of substrings in given string according to given queries?

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;
    }
}
  • 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);

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM