We help IT Professionals succeed at work.

countCode java challenge

Hi,
I am working on below challenge
http://codingbat.com/prob/p123614


I tried my code as below

public int countCode(String str) {
  
  
 
int x=countCodeString(str);

return x; 
    
}

public int countCodeString(String s){  
int count=0;
  
  for(int i=0;i<s.length();i++){
  
  if(s.substring(i,i+3).equals("code")){
  count=count++;
  }
  else
  return 100;
  
  }
  return count;
  }

Open in new window


My test results are
Expected      Run            
countCode("aaacodebbb") → 1      100      X         
countCode("codexxcode") → 2      100      X         
countCode("cozexxcope") → 2      100      X         
countCode("cozfxxcope") → 1      100      X         
countCode("xxcozeyycop") → 1      100      X         
countCode("cozcop") → 0      100      X         
countCode("abcxyz") → 0      100      X         
countCode("code") → 1      100      X         
countCode("ode") → 0      100      X         
countCode("c") → 0      Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 3 (line number:16)      X         
countCode("") → 0      0      OK         
countCode("AAcodeBBcoleCCccoreDD") → 3      100      X         
countCode("AAcodeBBcoleCCccorfDD") → 2      100      X         
countCode("coAcodeBcoleccoreDD") → 3      100      X         
other tests
X       


how to  improve my approach, results and design of this challenge. How do i

make a graphical venn or flow chart or some other relevant diagram to design it

before writing code to decide best strategy?
 Please advise
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
public int countCode(String str) {
  return str.length()-str.replaceAll("co.e","coe").length();
}
Amitkumar PSr. Consultant
CERTIFIED EXPERT

Commented:
Agree with ozo. Perfect solution.

Good combination of regex + logic. :)
CERTIFIED EXPERT
Commented:
Here is a variation of your OP. It still needs work.
public int countCode(String str) {
  int x=countCodeString(str);
  return x; 
}

public int countCodeString(String s){  
int count=0;
  
  for(int i=0; i <= s.length() - 4; i++)   // if s.length() = 4, you only need one pass in the loop
  {
    if(s.substring(i,i+4).equals("code"))
    {
       count += 1; // this is functionally very different than what you originally wrote.
    }
  }
  return count;
}

Open in new window

Now that this shows some improvement, throw it away and work one char at a time as shown in the corresponding tutorial:

This twoE() example method returns true if the string contains exactly two 'e' chars. The code:
"for (int i=0; i<str.length(); i++) { ..." is very standard code to look at each position in a String.
public boolean twoE(String str) {
  int count = 0;
  for (int i=0; i<str.length(); i++) {
    String sub = str.substring(i, i+1);
    if (sub.equals("e")) count++;
  }
  if (count == 2) return true;
  return false;
  // last 2 lines can be written simply as "return (count == 2);"
}

Open in new window

http://codingbat.com/doc/java-example-solution-code.html

You check for a 'c'. If not, continue, and check again for a 'c'. If found, then check for an 'o'. If not an 'o', then start checking for a 'c' again. If 'c' followed by 'o' is found, then skip the next char since it can be anything. Then check for an 'e'. If found, increment the counter.
CERTIFIED EXPERT

Commented:
What IDE and debugger are you using to help you understand the bugs in your code. If you are simply putting your best effort into the challenge box, then you are not going to complete all the challenges in an efficient manner. There is a lot of benefit of stepping through your code and checking each line to confirm the statement does what you intended. Netbeans and Eclipse are two popular Java IDE that will allow you to step through your code and debug it.

Author

Commented:
i am using eclipse.
CERTIFIED EXPERT

Commented:
With eclipse, you can step through the program one line at a time. Before you step, you should expect a certain value if assignment, or branch for an if-statement. If you got it wrong, then try to understand what went wrong with your expected outcome. If the code's outcome is wrong for a line, it could be that the line is correct but a previously executed line was wrong.

Are you using this approach or something similar? If so, then your posts could be about specific lines of code giving you results that you do not expect.

I wrote in the code post:
count += 1; // this is functionally very different than what you originally wrote.

If you used the above debugging methodology, I bet you would have spotted the problem.

Author

Commented:
public class CountCodeEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("values is--->"+countCode("aaacodebbb"));

	}

	
	
	public static int countCode(String str) {
		  
		  
		 
		int x=countCodeString(str);

		return x; 
		    
		}

		public static int countCodeString(String s){  
		int count=0;
		  
		  for(int i=0;i<s.length()-3;i++){
		  //how to verify a to z here in the position of d of code?? put || condition 26 times for each letter of alphabet
		  if(s.substring(i,i+4).equals("code")||s.substring(i,i+4).equals("cope")||s.substring(i,i+4).equals("coze")){
		  count=count+1;
		  continue;
		  }
		  else
		  count=count;
		  continue;
		  
		  }
		  return count;
		  }

}

Open in new window


i have not used eclipse debugger for this challenge till now. I just used it and fixed my code as below
public class CountCodeEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("values is--->"+countCode("aaacodebbb"));

	}

	
	
	public static int countCode(String str) {
		  
		  
		 
		int x=countCodeString(str);

		return x; 
		    
		}

		public static int countCodeString(String s){  
		int count=0;
		  
		  for(int i=0;i<s.length()-3;i++){
		  //how to verify a to z here in the position of d of code?? put || condition 26 times for each letter of alphabet
		  if(s.substring(i,i+4).equals("code")||s.substring(i,i+4).equals("cope")||s.substring(i,i+4).equals("coze")){
		  count=count+1;
		  continue;
		  }
		  else
		  count=count;
		  continue;
		  
		  }
		  return count;
		  }

}

Open in new window


I passed all tests as below

public int countCode(String str) {
  
  
// public static int countCode(String str) {
		  
		  
		 
		int x=countCodeString(str);

		return x; 
		    
		}

		public int countCodeString(String s){  
		int count=0;
		  
		  for(int i=0;i<s.length()-3;i++){
		  //how to verify a to z here in the position of d of code?? put || condition 26 times for each letter of alphabet
		  if(s.substring(i,i+4).equals("code")||s.substring(i,i+4).equals("cope")||s.substring(i,i+4).equals("coze")||s.substring(i,i+4).equals("cole")||s.substring(i,i+4).equals("core")||s.substring(i,i+4).equals("cofe")){
		  count=count+1;
		  continue;
		  }
		  else
		  count=count;
		  continue;
		  
		  }
		  return count;
		  }

Open in new window


Expected	Run		
countCode("aaacodebbb") → 1	1	OK	    
countCode("codexxcode") → 2	2	OK	    
countCode("cozexxcope") → 2	2	OK	    
countCode("cozfxxcope") → 1	1	OK	    
countCode("xxcozeyycop") → 1	1	OK	    
countCode("cozcop") → 0	0	OK	    
countCode("abcxyz") → 0	0	OK	    
countCode("code") → 1	1	OK	    
countCode("ode") → 0	0	OK	    
countCode("c") → 0	0	OK	    
countCode("") → 0	0	OK	    
countCode("AAcodeBBcoleCCccoreDD") → 3	3	OK	    
countCode("AAcodeBBcoleCCccorfDD") → 2	2	OK	    
countCode("coAcodeBcoleccoreDD") → 3	3	OK	    
other tests
OK	 

Open in new window


how to improve my approach.

i wonder how to take care of all 26 characters instead of d within 'code'
also why i have to use code once in if loop and then in the else loop to work. Do i need else loop here?
when to use break rather than continue?

Author

Commented:
public int countCode(String str) {


	// public static int countCode(String str) {



	int x=countCodeString(str);

	return x; 

}

public int countCodeString(String s){  
	int count=0;

	for(int i=0;i<s.length()-3;i++){
		//how to verify a to z here in the position of d of code?? put || condition 26 times for each letter of alphabet
		if(s.substring(i,i+4).equals("code")||s.substring(i,i+4).equals("cope")||s.substring(i,i+4).equals("coze")||s.substring(i,i+4).equals("cole")||s.substring(i,i+4).equals("core")||s.substring(i,i+4).equals("cofe")){
			count=count+1;
			continue;
		}


	}
	return count;
}

Open in new window


my code is happy without the else also?

Author

Commented:
return str.length()-str.replaceAll("co.e","coe").length();

what is logic behind above code?

if string is aaacodebbb
then
we are finding the length which is  10
now  replacing anything co.e(where . represents any character according to regular expression??) with coe so new string becomes aaacoebbb andnew length is 9 so 10-9 is 1 so one co.e is there in the given string??
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Correct.
CERTIFIED EXPERT

Commented:
public String replaceAll(String regex, String replacement)
This is creating a new string. If you have a long text, you are creating a long new string. This could be a serious problem in a HPC environment.