Solved

strCount chalenge

Posted on 2016-09-23
3
82 Views
Last Modified: 2016-09-23
Hi,

I am working on below challenge.
http://codingbat.com/prob/p186177
Psedo code:
1. check  if str has sub string if yes increment by 1 or repeat

I wrote my code as below and not passing all tests
public int strCount(String str, String sub) {
  if (str.length() <2){ 
	    	return 0;
	    		}
	    if (str.startsWith( sub) {
	    	return 1 + strCount(str.substring(2));
	    	}
	    	else {
		   return strCount(str.substring(1));
	    	  
	    	}
}

Open in new window


Compile problems:


Error:      if (str.startsWith( sub) {
                             ^
Syntax error on token ")", ) expected after this token


see Example Code to help with compile problems

Any improvements or alternate approaches?      

please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 7

Author Comment

by:gudii9
ID: 41812763
public int strCount(String str, String sub) {
  if (str.length() <sub.length()){ 
	    	return 0;
	    		}
	    if ((str.substring(0,sub.length()).equals(sub))) {
	    	return 1 + strCount(str.substring(sub.length()),sub);
	    	}
	    	else {
		   return strCount(str.substring(1),sub);
	    	  
	    	}
}

Open in new window

above passed all tests. an improvements or alternate approaches?
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 41812849
Here is my code.
public int strCount(String str, String sub) {
  int index = str.indexOf(sub);
  if(index == -1 || str.length() < sub.length())return 0;
  return 1 + strCount(str.substring(index + sub.length()), sub);
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 41812892
i like above approach
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

831 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