String[] splitArray = str.split("(?<=[dog,cat])");
int dogCounter = 0;
int catCounter = 0;
for(int i = 0; i < splitArray.length; i++) {
if (splitArray[i] == "dog") {
dogCounter += 1;
}
else if (splitArray[i] == "cat") {
catCounter += 1;
}
}
if (dogCounter == catCounter) {
return true;
}
else {
return false;
}
}
So this code essentially takes the string provided when running the function, and splits it at dog and cat but without actually removing dog or cat from the string, then assigns this to an array, loops through the array determining if any of the items equal cat or dog and adding them to their respective variables if so, then if cat and dog are both in the sting an equal amount of times it is supposed to return true, and if not, return false. But it always returns true no matter what.