Programmers learn & share
0 votes

Problem :

I am facing following issue need solution
return true if the string "cat" and "dog" appear the same number of times in the given string
by (6.9k points)   | 107 views

2 Answers

0 votes

Solution :

Following is my answer. It is working as per need. Hope it helps you.

public boolean catDogString(String str) {
  int cat = 0 ;
  int dog = 0 ;
  for(int s  = 0 ; s < str.length()-2; s++){
    String temp = str.substring(s,s+3);
  return cat==dog?true:false;
by (36.1k points)  
0 votes


List of  few critical problems in your code

1. Your call to substring() fetches a string of size 2. That string can never be cat nor dog. Change the second parameter in the               method call to i + 3 to get 3 characters.


2. Your call to substring() will throw an IndexOutOfRangeException, as i approaches the end of the length of the input string.              Make sure you do not ask for a substring that "overflows" the length of the input string. One way to fix this bug, would be to change        the conditional expression in your for loop to i < str.length() - 2.


3. The return value of your method will always be true. In the case where dogAnswer != catAnswer you return exactly that                    expression - which will resolve to true. A possible solution: Merge the two return statements into return dogAnswer ==          catAnswer.

Additionally, there are a few things you could do to make your code simpler:

1. There really is no need to copy cat_Count into catAnswer and dog_Count into dogAnswer. Throw away two of the variables, and        use the other pair exclusively.

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

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);

You will want to iterate each character in your string and then check to see if the substring is equal to either cat or dog and increments the counts.

public boolean catDog(String str) {

  int end = str.length();
  int cDog = 0;
  int cCat = 0;

  // We don't need to go to the end because cat/dog are 3 characters long each. So we can avoid extra checks.
  for (int i=0; i < end-2; i++) {
        if(str.substring(i, i+3).equals("cat")) {
        } else if(str.substring(i, i+3).equals("dog")) {

   return cCat == cDog ? true : false;

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.

public boolean catDog(String str) {
    int cdog = 0;
    int ccat = 0;

    for (int i = 0; i < str.length()-2; i++)
        if (str.charAt(i)== 'c' && str.charAt(i+1)== 'a' && str.charAt(i+2)== 't')

        else if(str.charAt(i) == 'd' && str.charAt(i+1)== 'o' && str.charAt(i+2)== 'g')


    if (cdog == ccat)
        return true;

        return false;


by (11.2k points)  
2,227 questions
2,734 answers
241 users