title: 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?
body: <p>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.</p>
  <p><strong>Example</strong></p>
  <p>Assumptions</p>
  <p>strr = [&quot;asdfc&quot;, &quot;asfc&quot;, &quot;vdsfc&quot;, &quot;trgfds&quot;, &quot;egregds&quot;, &quot;tertdfc&quot;, &quot;rtyergds&quot;]</p>
  <p>findStr = [&quot;dfc&quot;, &quot;fc&quot;, &quot;ds&quot;]</p>
  <p>Approach:</p>
  <p>In the 1st Query, the required suffix is &quot;dfc&quot;. The strings that have this suffix are [asdfc, tertdfc]. Hence, the count is 2.</p>
  <p>In 2nd query, the required suffix is &quot;fc&quot;. The strings that have this suffix are [asdfc, asfc, vdsfc, tertdfc]. Hence, the count is 4.</p>
  <p>In 3rd query, the required suffix is &quot;ds&quot;. The strings that have this suffix are [trgfds, egregds, rtyergds]. Hence, the count is 3.</p>
  <p><strong>Hence the output is [2,4,3].</strong></p>
  <p>But When I'm trying to do this using my code I'm getting <strong>wrong output [2,6,10]</strong>. Can anyone tell me what I'm doing wrong here ?</p>
  <pre><code>class FindCount {
      public static void main(String[] args) {
          String[] strr = new String[]{&quot;asdfc&quot;, &quot;asfc&quot;, &quot;vdsfc&quot;, &quot;trgfds&quot;, &quot;egregds&quot;, &quot;tertdfc&quot;, &quot;rtyergds&quot;};
  
          String[] findStr = new String[]{&quot;dfc&quot;, &quot;fc&quot;, &quot;ds&quot;};
          int count = 0;
          int result[] = new int[findStr.length];
  
          for (int i = 0; i &lt; findStr.length; i++) {
              for (int j = 0; j &lt; 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;
      }
  }
  </code></pre>