Solved

strCount chalenge

Posted on 2016-09-23
3
51 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now