views:

358

answers:

4

Could anyone check my solution?

I want to return true if the string "cat" and "dog" appear the same number of times in the given string. There are various strings with different numbers of "cat" and "dog".

public boolean catDog(String str) 
{  
  int catAnswer = 0;
  int dogAnswer = 0;
  int cat_Count = 0;
  int dog_Count = 0;

  for (int i=0; i< str.length()-1; i++) 
  {
    String sub = str.substring(i, i+2);


    if ((sub.equals("cat")))  cat_Count++;
    if ((sub.equals("dog")))  dog_Count++;
    catAnswer = cat_Count; 
    dogAnswer = dog_Count;

  } //end for

  if(dogAnswer == catAnswer ) {return true;}
  // else
  return (dogAnswer != catAnswer);
}

UPDATE:

  1. If i use i + 3 i get an error code Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 7 (line number:10) - hence i use i + 2 (no errors are reported with it

  2. Changing to i < str.length() - 4 gives a blanket response of true despite some of the test strings containing an unequal number of "cat" & "dog " tokens

The crux of the problem is that the response is either all true or all false when there is variable numbers of "cat" & "dog" in the various strings

the output from the code can be seen at http://codingbat.com/prob/p111624 - catDog string problem

Please try in cutting and pasting my code to see the output - this will explain more graphically than i could say

+4  A: 
Jørn Schou-Rode
Rats, you beat me. I'd typed up pretty much the same answer but didn't refresh the page quick enough.
Michael Myers
The loop limit should not be `str.length() - 4`, it should be `str.length() - 2`. Otherwise you'll miss a cat or dog appearing at the very end of the string.
Michael Myers
mmyers: Nope. str.length() - 3 should be the last index. Think of the string "cat". Do you want to call "cat".substring(1, 4) ?
roryparle
@mmeyers: Heh, I started typing up my answer just after casting my vote to reopen, as a compensation for the early close. I really have no idea how I ended up at `str.length() - 4`, but at least it is fixed now, thanks to you :)
Jørn Schou-Rode
@roryparle: Note the use of "explicitly less than" `<` in both the original code and the answer. Using `<=` and `3` might make the code more obvious though.
Jørn Schou-Rode
@roryparle: I did test it before commenting, just in case I'd made an off-by-one error.
Michael Myers
A: 

1. if i use i + 3 i get an error code Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: 7 (line number:10) - hence i use i + 2 (no errors are reported with it 2.changing to i < str.length() - 4 gives a blanket response of true despite some of the test strings containing an unequal number of "cat" & "dog " tokens

The crux of the problem is that the response is either all true or all false despite there being a variable numbers of "cat" & "dog" tokens in the various strings

the output from the code can be seen at http://codingbat.com/prob/p111624 - catDog string problem

Please try in cutting and pasting my code to see the output - this will explain more graphically than i could say

stanny110
You'd do better to edit your post and add information, rather than adding an answer. I've already done that, but what you can do _now_ is delete this answer before _it_ gets downvotes....
Michael Todd
@stanny: `i < str.length() - 4` was a blunder by me, and it has been fixed (see comments on my answer). You will get exceptions if you follow my advice #1 alone. Implement #2 as well to make the exceptions go away, and #3 to get the right return values.
Jørn Schou-Rode
@Michael Todd: Looks like he's lost the cookie, so he can't edit the question.
Michael Myers
A: 

A solution with less duplication would be to write a function that counts occurrences of a substring within a string, then call that function with the string and "cat", and with the string and "dog", and return whether the two counts agree.

Carl Manaster
+1  A: 
int count(String needle, String haystack) {
  return haystack.split(needle, -1).length - 1;
}

public boolean catDog(String str) {
  return count("dog", str) == count("cat", str);
}

Here's an "All Correct" solution that uses split. The -1 is used to preserve trailing empty strings.

polygenelubricants