Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# countAbc challenge

Posted on 2016-09-21
Medium Priority
71 Views
Hi,

I am working on below challenge.
http://codingbat.com/prob/p161124

I wrote as below and failing some tests.
Expected      Run
countAbc("abc") → 1      0      X
countAbc("abcxxabc") → 2      0      X
countAbc("abaxxaba") → 2      0      X
countAbc("ababc") → 2      1      X
countAbc("abxbc") → 0      0      OK
countAbc("aaabc") → 1      0      X
countAbc("hello") → 0      0      OK
countAbc("") → 0      0      OK
countAbc("ab") → 0      0      OK
countAbc("aba") → 1      0      X
countAbc("aca") → 0      0      OK
countAbc("aaa") → 0      0      OK
other tests
X

``````public int countAbc(String str) {

CharSequence cs1="abc";
CharSequence cs2="aba";
if(str.length()==0||str.length()==1||str.length()==2){
return 0;
}
if(!str.contains(cs1)||!str.contains(cs2)){
return 0;
}
else return 1+countAbc(str.substring(3));
}
``````

0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3

LVL 7

Author Comment

ID: 41809446
``````public int countAbc(String str) {

CharSequence cs1="abc";

CharSequence cs2="aba";
if(str.length()==0||str.length()==1||str.length()==2){
return 0;
}

else if(!str.contains(cs1)||!str.contains(cs2)){
return 0;
}
else return 1+countAbc(str.substring(1));
}
``````

above also failing some tests
0

LVL 37

Accepted Solution

zzynx earned 1000 total points
ID: 41810187
This works:

``````public int countAbc(String str) {
String stringABC = "abc";
String stringABA = "aba";

if (str.length()==0||str.length()==1||str.length()==2){
return 0;
}

if(str.startsWith(stringABC)){
return 1 + countAbc(str.substring(3));
} else if(str.startsWith(stringABA)){
return 1 + countAbc(str.substring(2));
}
return countAbc(str.substring(1));
}
``````
0

LVL 35

Assisted Solution

ste5an earned 1000 total points
ID: 41810195
``````(str.length()==0||str.length()==1||str.length()==2)
``````

Really?
0

LVL 37

Expert Comment

ID: 41810211
No.
``````if (str.length()<3)
``````
:)
0

LVL 7

Author Comment

ID: 41810797
``````  else if(!str.contains(cs1)||!str.contains(cs2)){
``````

contains charsequence approach wont work here?
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
0

LVL 7

Author Comment

ID: 41810806
``````public int countAbc(String str) {
String stringABC = "abc";
String stringABA = "aba";

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

if(str.startsWith(stringABC)){
return 1 + countAbc(str.substring(3));
} else if(str.startsWith(stringABA)){
return 1 + countAbc(str.substring(2));
}
return countAbc(str.substring(1));
}
``````

startsWith how is working here why we are moving to str.substring(2));

str.substring(1));
0

LVL 7

Author Comment

ID: 41810811
return 1 + countAbc(str.substring(3));
why one return has 3 in if loop as above and one has 2 as below in else if?

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

abc need not be at start right? it could be at middle also?
0

LVL 37

Expert Comment

ID: 41810949
If we find the substring "aba", that last 'a' could be the start of another "aba" or "abc" and the challenge wants us to count that. That's why
0

LVL 7

Author Comment

ID: 41811225
got it
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
###### Suggested Courses
Course of the Month5 days, 21 hours left to enroll