Problem:
Given a string, return true if it ends in "ly".
endsLy("oddly") → true
endsLy("y") → false
endsLy("oddy") → false
Solution:
public boolean endsLy(String str) { int len = str.length(); String ly = "ly"; if (len < 2) return false; else if (ly.equals(str.substring(len-2,len))) return true; else return false; }
This below code will also work.
ReplyDeletepublic boolean endsLy(String str) {
if(str.length() <2 ){
return false;
}
if(str.substring(str.length()-2).equals("ly"))
return true;
else
return false;
}
public boolean endsLy(String str) {
ReplyDeleteif(str.length() < 2){
return false;
}
return(str.substring(str.length()-2).equals("ly"));
}
public boolean endsLy(String str) {
ReplyDeletereturn str.endsWith("ly");
}
public boolean endsLy(String str) {
ReplyDeleteint len = str.length();
return (len>=2) && str.substring(len-2, len).equals("ly");
}
public boolean endsLy(String str) {
ReplyDeleteif(str.length()<2){
return false;
}else{
String a=str.substring(str.length()-2);
String ly="ly";
if(ly.equals(a)){
return true;
}return false;
}
}
public boolean endsLy(String str) {
ReplyDeleteif(str.length()<2){
return false;
}else{
String a=str.substring(str.length()-2);
String ly="ly";
if(ly.equals(a)){
return true;
}return false;
}
}
if(str.length() < 2) return false;
ReplyDeletereturn (str.substring(str.length()-2).equals("ly"));
public boolean endsLy(String str) {
ReplyDeletereturn (str.length()>1&&str.substring(str.length()-2,str.length()).equals("ly"));
}
public boolean endsLy(String str) { if(str.length()>=2) { if(str.substring(str.length()-2,str.length())==("ly")) { return true; } else { return false; } } else { return false; } }
ReplyDeletewhat is the error..!explain in detail
ReplyDeletepublic boolean endsLy(String str) {
ReplyDeleteint length=str.length();
if(length<2)
return false;
if(str.charAt(length-1)=='y' && str.charAt(length-2)=='l')
return true;
else
return false;
}
public boolean endsLy(String str) {
ReplyDeleteif(str.endsWith("ly")){
return true;
}else return false;
}
public boolean endsLy(String str) {
ReplyDeleteint len=str.length()-2;
if(str.length()<2) return false;
String s=str.substring(len);
if(s.equals("ly")){
return true;
}
return false;
}
return str.endsWith("ly") ? true:false;
ReplyDelete