Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding a String in another String Using Recursion Mechanism in Core Java
    text
    copied!<p>I have the below Problem Statement</p> <p><strong>PS: Given a string "str" and a Non-Empty substring "sub" ,compute "Recursively" if at least "N" copies of "sub" appear in the "string somewhere", possibly with "Overlapping". N will be non-negative.</strong> </p> <p><code>Example are as shown below</code><br> <code>strCopies("catcowcat", "cat", 2) → true</code><br> <code>strCopies("catcowcat", "cow", 2) → false</code><br> <code>strCopies("catcowcat", "cow", 1) → true</code><br> <code>strCopies("iiijjj", "ii", 2) → true</code> </p> <p>I have written the code as shown below(without recursion) and is working fine for few test cases,except for others which are marked as FAIL.</p> <p><strong>:::Code is as shown below:::</strong></p> <pre><code>public boolean strCopies(String str, String sub, int n) { int len = sub.length(); int result=0; if(len&gt;0){ int start = str.indexOf(sub); while(start !=-1){ result++; start = str.indexOf(sub,start+len); } } if(result==n){ return true; }else return false; } </code></pre> <p>Runs for above code as shown below(Marked in BOLD are FAILED TEST CASES) </p> <p><code>Expected This Run</code><br> <code>strCopies("catcowcat", "cat", 2) → true true OK</code><br> <code>strCopies("catcowcat", "cow", 2) → false false OK</code><br> <code>strCopies("catcowcat", "cow", 1) → true true OK</code><br> <code>strCopies("iiijjj", "ii", 2) → true false FAIL</code><br> <code>strCopies("iiiiij", "iii", 3) → true false FAIL</code><br> <code>strCopies("ijiiiiij", "iiii", 2) → true false FAIL</code></p> <p><strong>Could you check and let me know what is wrong with the code for FAIL TEST CASES ?Im unable to consider the Overlapping scenarios.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload