Solved

count11 challenge

Posted on 2016-09-21
6
90 Views
Last Modified: 2016-09-22
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));

}

Open in new window


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?      

please advise
0
Comment
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
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 41810180
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));
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41810814
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));

}

Open in new window


below also fails?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41810929
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));
}

Open in new window


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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 41810962
>> i like above approach
OK. Good. :)

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

Open in new window

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

Open in new window


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

Author Comment

by:gudii9
ID: 41811219
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 41811979
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust the codes 3 63
C++ statement T∗ begin(Vector<T>& x) 5 42
convert Absolute path to relative path of FTP location 7 50
Firefox and firebub 5 26
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question