## Problem:

Count recursively the total number of "abc" and "aba" substrings that appear in the given string.

countAbc("abc") → 1
countAbc("abcxxabc") → 2
countAbc("abaxxaba") → 2

## Solution:

public int countAbc(String str) {
if (str.length() < 3) return 0;
if (str.substring(0,3).equals("abc") || str.substring(0,3).equals("aba"))
return 1 + countAbc(str.substring(1));
else
return countAbc(str.substring(1));
}

1. wouldn't it be more efficient in line 4 to make it substring(2) as its impossible for the second place to be the start of another "true" statement.

2. public int countAbc(String str) {
if (str.length()<3)
return 0;

int count = 0;
if (str.substring(0,2).equals("ab") && (str.charAt(2)=='a' || str.charAt(2)=='c'))
count++;

return count + countAbc(str.substring(1));
}

3. public int countAbc(String str) {
if (str.length() < 3)
{
return 0;
}
else
{
if (str.substring(0, 3).equals("abc") || str.substring(0, 3).equals("aba"))
{
return 1 + countAbc(str.substring(2));
}
else
{
return countAbc(str.substring(1));
}
}
}

4. public int countAbc(String str) {
if(str.isEmpty()){
return 0;
}
if (str.length() >= 3
&& str.charAt(0) == 'a'
&& str.charAt(1) == 'b'
&& str.charAt(2) == 'a'
!= (str.charAt(2) == 'c')){
return 1 + countAbc(str.substring(2));
}
return countAbc(str.substring(1));
}

5. public int countAbc(String str) {

if (str.length() < 3) return 0;

if (str.substring(0,3).equals("abc") || str.substring(0,3).equals("aba"))

return 1 + countAbc(str.substring(1));

else

return countAbc(str.substring(1));

}

6. public int countAbc(String str) {
if(str.length() < 3) return 0;
if(str.startsWith("abc") || str.startsWith("aba"))
return 1 + countAbc(str.substring(2));
return countAbc(str.substring(1));
}