Java > String-2 > catDog (CodingBat Solution)

Problem:

Return true if the string "cat" and "dog" appear the same number of times in the given string.

catDog("catdog") → true
catDog("catcat") → false
catDog("1cat1cadodog") → true


Solution:

public boolean catDog(String str) {
  int len = str.length();
  int cat = 0;
  int dog = 0;
  
  for (int i = 0; i < len - 2; i++) {
    String temp = str.substring(i, i+3);
    if (temp.compareTo("cat") == 0)
      cat++;  
    if (temp.compareTo("dog") == 0)
      dog++;  
  }
  if (cat == dog)
    return true;
  else
    return false;
}


20 comments :

  1. of all the "answers" on the web, this is the only one that worked! (it took one obvious change but other than that it was great)

    ReplyDelete
    Replies
    1. public boolean catDog(String str) {
      int cat=0; int dog=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'){
      cat++;
      }else if(str.charAt(i)=='d'&&str.charAt(i+1)=='o'&&str.charAt(i+2)=='g'){
      dog++;
      }
      }
      if(cat==dog)return true;
      else
      return false;
      }

      Delete
  2. public boolean catDog(String str) {
    int c=0;
    for (int i=0;i<str.length()-2;i++) {
    if (str.substring(i,i+3).equals("cat")) c++;
    if (str.substring(i,i+3).equals("dog")) c--;
    }
    return (c==0);
    }

    ReplyDelete
  3. public boolean catDog(String str) {
    int c=0;
    int d=0;

    for(int i=0;i<str.length()-2;i++)
    {
    if(str.substring(i,i+3).equals("cat"))
    c = ++c;
    if(str.substring(i,i+3).equals("dog"))
    d = ++d;

    }
    if(c==d)
    {
    return true;
    }
    return false;
    }

    ReplyDelete
  4. THIS IS MY SOLUTION

    int catcount = 0;
    int dogcount = 0;


    for(int i = 0; i < str.length()- 2; i++){
    if(str.substring(i, i+3).equals("cat")){
    catcount++;
    }

    if(str.substring(i, i+3).equals("dog")){
    dogcount++;

    }
    }

    if(catcount == dogcount){
    return true;
    }

    return false;
    }

    ReplyDelete
  5. int cat = 0;
    int dog = 0;
    for (int i = 0; i < str.length() - 2; i++){
    if(str.substring(i, i + 3).equals("cat"))
    cat++;
    if(str.substring(i, i + 3).equals("dog"))
    dog++;
    }
    return (cat == dog);

    ReplyDelete
  6. public boolean catDog(String str) {
    return str.replace("dog", "").length() ==
    str.replace("cat", "").length();
    }

    ReplyDelete
    Replies
    1. this some advance code for me atleast can you explain this? I did it Long way

      Delete
    2. your code is very good plz.. explain it

      Delete
  7. public boolean catDog(String str) {
    int cat=0;
    int dog=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')cat++;
    if(str.charAt(i)=='d'&&str.charAt(i+1)=='o'&&str.charAt(i+2)=='g')dog++;
    }
    return dog==cat;
    }

    ReplyDelete
  8. public boolean catDog(String str) {
    int countCat = 0;
    int countDog = 0;

    for(int i=0 ; i<str.length()-2 ; i++) {
    if(str.substring(i,i+3).equals("cat")) countCat++;
    if(str.substring(i,i+3).equals("dog")) countDog++;
    }
    return countCat==countDog;
    }

    ReplyDelete
  9. public boolean catDog(String str) {
    int countCat = 0;
    int countDog = 0;
    for(int i=0; i<str.length()+1; i++){
    if(str.substring(i).startsWith("cat")) {
    countCat++;
    }
    if(str.substring(i).startsWith("dog")){
    countDog++;
    }
    }
    if(countCat==countDog) {
    return true;
    }
    return false;

    }

    ReplyDelete
  10. public boolean catDog(String str) {
    return str.replace("dog", "").length() == str.replace("cat", "").length();
    }

    ReplyDelete
  11. public boolean catDog(String str) {
    int countC=0;
    // int countD=0;
    for(int i=0;i<str.length()-2;i++){
    if(str.substring(i,i+3).equals("cat")){
    countC++;
    }
    }
    for(int j=0;j<str.length()-2;j++){
    if(str.substring(j,j+3).equals("dog")){
    countC--;
    }
    }

    return (countC==0);
    }

    ReplyDelete
  12. public boolean catDog(String str) {
    int countCat = 0;
    int countDog = 0;

    for (int i = 0; i < str.length() - 2; i++) {

    if (str.substring(i, i + 3).equals("cat")) {
    countCat++;
    }

    if (str.substring(i, i + 3).equals("dog")) {
    countDog++;
    }
    }
    return (countCat == countDog);
    }

    ReplyDelete
  13. int cat1=0;
    int dog1=0;

    for (int i=0;i<str.length()-2;i++)
    {
    String s=str.substring(i,i+3);
    if (s.equals("cat")) cat1++;
    if (s.equals("dog")) dog1++;
    }
    if (cat1 == dog1)
    {return true;}
    else
    {return false;}

    ReplyDelete
  14. public boolean catDog(String str) {
    int c=0;
    int d=0;
    for(int i=0;i<str.length()-2;i++)
    if(str.substring(i,i+3).equals("cat"))
    c++;
    for(int i=0;i<str.length()-2;i++)
    if(str.substring(i,i+3).equals("dog"))
    d++;
    return (c==d);
    }

    ReplyDelete
  15. Another way

    public boolean catDog(String str) {
    int cat = str.split("cat", - 1).length - 1;
    int dog = str.split("dog", - 1).length - 1;
    return cat == dog;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact