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; }
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)
ReplyDeletepublic boolean catDog(String str) {
Deleteint 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;
}
public boolean catDog(String str) {
ReplyDeleteint 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);
}
why do people put length -2, why is it -2?
Deletepublic boolean catDog(String str) {
ReplyDeleteint 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;
}
THIS IS MY SOLUTION
ReplyDeleteint 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;
}
int cat = 0;
ReplyDeleteint 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);
public boolean catDog(String str) {
ReplyDeletereturn str.replace("dog", "").length() ==
str.replace("cat", "").length();
}
this some advance code for me atleast can you explain this? I did it Long way
Deleteyour code is very good plz.. explain it
Deletepublic boolean catDog(String str) {
ReplyDeleteint 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;
}
public boolean catDog(String str) {
ReplyDeleteint 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;
}
public boolean catDog(String str) {
ReplyDeleteint 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;
}
public boolean catDog(String str) {
ReplyDeletereturn str.replace("dog", "").length() == str.replace("cat", "").length();
}
public boolean catDog(String str) {
ReplyDeleteint 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);
}
Why is it -2 can someone explain it??
Deletepublic boolean catDog(String str) {
ReplyDeleteint 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);
}
int cat1=0;
ReplyDeleteint 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;}
public boolean catDog(String str) {
ReplyDeleteint 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);
}
Another way
ReplyDeletepublic boolean catDog(String str) {
int cat = str.split("cat", - 1).length - 1;
int dog = str.split("dog", - 1).length - 1;
return cat == dog;
}