Keep in mind that shorter code doesn't always mean faster running code. Your goal should be to utilize speed by finding the shortest path from point A to point B. Short code isn't good code if it runs slowly.
The code you provided probably doesn't compile. But, you can do a tradeoff of extra memory for less computations. When to do this depends on what the method is doing, but given you are just messing around with ints, you could have something like this:
int index = inputOne.indexOf(inputTwo);
if (index >= 0) { // IndexOf returns -1 if the String is not found
int diff = inputOne.length()-inputTwo.length();
if(diff>0) { // If inputOne.length() is > inputTwo.length, subtracting the two would be a positive number
return diff==index; // We have already calculated the difference and found the index, so we are saving a few computations
}
}
}
return false;
It's more costly to memory in the short-term, but given the values you are saving are very small in size, the extra use of memory should out-match the extra computations we didn't have to do.