Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 139

# count11 challenge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p167015
Psedo code:
1. check the array length 0  or 1 then retun 0
2.else if string does not consist of 11  return 0
3.else increment by 1 and calling same recursion function

I wrote my code as below and not passing all tests

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

CharSequence cs1="11";

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

else if(!str.contains(cs1)){
return 0;
}
else return 1+count11(str.substring(1));

}
``````

Expected      Run
count11("11abc11") → 2      5      X
count11("abc11x11x11") → 3      9      X
count11("111") → 1      1      OK
count11("1111") → 2      2      OK
count11("1") → 0      0      OK
count11("") → 0      0      OK
count11("hi") → 0      0      OK
count11("11x111x1111") → 4      9      X
count11("1x111") → 1      3      X
count11("1Hello1") → 0      0      OK
count11("Hello") → 0      0      OK
other tests
X

Any improvements or alternate approaches?

0
gudii9
• 3
• 3
1 Solution

Software engineerCommented:
This works

``````public int count11(String str) {
String string11 = "11";

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

if(str.startsWith(string11)){
return 1 + count11(str.substring(2));
}
return count11(str.substring(1));
}
``````
0

Author Commented:
how contains different from startsWith method?
why cannot i use contains method here?
``````public int count11(String str) {

CharSequence cs1="11";

//CharSequence cs2="aba";
if(str.length()<3){
return 0;
}

else if(!str.contains(cs1)){
return 0;
}
else return 1+count11(str.substring(2));

}
``````

below also fails?
0

Author Commented:
``````public int count11(String str) {
String string11 = "11";

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

if(str.startsWith(string11)){
return 1 + count11(str.substring(2));
}
return count11(str.substring(1));
}
``````

i like above approach checking if string 11 there then increment by 1 and shift two positions(as duplicats not needed) and call same recursion method
0

Software engineerCommented:
>> i like above approach
OK. Good. :)

Again, you can of course replace
``````if(str.length()==0||str.length()==1){
return 0;
}
``````
by
``````if(str.length()<2){
return 0;
}
``````

>> why cannot i use contains method
contains() versus startsWith()
The answer is in their names.
0

Author Commented:
>> why cannot i use contains method
contains() versus startsWith()
The answer is in their names.

can you please elaborate.

one to check if starts with a string
other if contains only charactersequence?
0

Software engineerCommented:
Starting with a certain string
or
containing a certain string
are two different things.
In this case of recurrence you really need the first one
0

## Featured Post

• 3
• 3
Tackle projects and never again get stuck behind a technical roadblock.