Sunday, May 24, 2015

CodingBat Java > Recursion-1 > strCount

Given a string and a non-empty substring sub, compute recursively the number of times that sub appears in the string, without the sub strings overlapping. 

strCount("catcowcat", "cat") → 2

strCount("catcowcat", "cow") → 1
strCount("catcowcat", "dog") → 0

Expected
Run
strCount("catcowcat", "cat") → 2
2
OK
strCount("catcowcat", "cow") → 1
1
OK
strCount("catcowcat", "dog") → 0
0
OK
strCount("cacatcowcat", "cat") → 2
2
OK
strCount("xyx", "x") → 2
2
OK
strCount("iiiijj", "i") → 4
4
OK
strCount("iiiijj", "ii") → 2
2
OK
strCount("iiiijj", "iii") → 1
1
OK
strCount("iiiijj", "j") → 2
2
OK
strCount("iiiijj", "jj") → 1
1
OK
strCount("aaabababab", "ab") → 4
4
OK
strCount("aaabababab", "aa") → 1
1
OK
strCount("aaabababab", "a") → 6
6
OK
strCount("aaabababab", "b") → 4
4
OK
other tests
OK




          public int strCount(String str, String sub) {

              int index = str.indexOf(sub);

              if (index < 0)
                     return 0;

              return 1 + strCount(str.substring(index + sub.length()), sub);
       }

No comments :

Post a Comment