public boolean endsLy(String str) {
int len=str.length();
if(str.substring(len-2).equals("ly")&& len>=2)
{
return true;
}
return false;
}
Expected Run
endsLy("oddly") → true true OK
endsLy("y") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:5) X
endsLy("oddy") → false false OK
endsLy("oddl") → false false OK
endsLy("olydd") → false false OK
endsLy("ly") → true true OK
endsLy("") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:5) X
endsLy("falsey") → false false OK
endsLy("evenly") → true true OK
other tests
i would like to know how can improve on my above code. public boolean endsLy(String str) {
int len=str.length();
if(len>=2 && str.substring(len-2).equals("ly"))
{
return true;
}
return false;
}
Expected Run
endsLy("oddly") → true true OK
endsLy("y") → false false OK
endsLy("oddy") → false false OK
endsLy("oddl") → false false OK
endsLy("olydd") → false false OK
endsLy("ly") → true true OK
endsLy("") → false false OK
endsLy("falsey") → false false OK
endsLy("evenly") → true true OK
other tests
OK
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
if(len>=2 && str.substring(len-2).equals("ly"))
if(len>=2 & str.substring(len-2).equali would have avoided this bottle neck. please advises("ly"))
public boolean endsLy(String str) {
int len=str.length();
if(str.substring(len-2).equals("ly") & len>=2 )
{
return true;
}
return false;
}
Expected Run
endsLy("oddly") → true true OK
endsLy("y") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:5) X
endsLy("oddy") → false false OK
endsLy("oddl") → false false OK
endsLy("olydd") → false false OK
endsLy("ly") → true true OK
endsLy("") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:5) X
endsLy("falsey") → false false OK
endsLy("evenly") → true true OK
other tests
OK
public static boolean endsLy(String str) {Expected Run
StringBuilder sb = new StringBuilder(str);
return sb.reverse().substring(0, 2).equalsIgnoreCase("yl");
}
public boolean endsLy(String str) {
int len=str.length();
if(len>=2 &str.substring(len-2).equals("ly") )
{
return true;
}
return false;
}
ndsLy("oddly") → true true OK
endsLy("y") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1 (line number:5) X
endsLy("oddy") → false false OK
endsLy("oddl") → false false OK
endsLy("olydd") → false false OK
endsLy("ly") → true true OK
endsLy("") → false Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -2 (line number:5) X
endsLy("falsey") → false false OK
endsLy("evenly") → true true OK
other tests
I like stringbuilder solution too.
does not call for case insensitivity
public boolean endsLy(String str) {
StringBuilder sb = new StringBuilder(str);
// return sb.reverse().substring(0, 2).equalsIgnoreCase("yl");
return sb.reverse().toString().startsWith("ly");
}
when i wrote as above it failedExpected Run
endsLy("oddly") → true false X
endsLy("y") → false false OK
endsLy("oddy") → false false OK
endsLy("oddl") → false false OK
endsLy("olydd") → false false OK
endsLy("ly") → true false X
endsLy("") → false false OK
endsLy("falsey") → false false OK
endsLy("evenly") → true false X
other tests
X
nor does it test any cases that end in "yl" so it might also be argued that sorting before testing could also be valid.i think it is failing if sorting or ordering(what is the difference?") is changing as above.
Then again, while it doesn't explicitly specify the the case of "ly" must be exact, neither does it explicitly specify that the order of "ly" must be exact, nor does it test any cases that end in "yl" so it might also be argued that sorting before testing could also be valid.