Problem:
Given a string, return true if the first 2 chars in the string also appear at the end of the string, such as with "edited".
frontAgain("edited") → true
frontAgain("edit") → false
frontAgain("ed") → true
Solution:
public boolean frontAgain(String str) { if (str.length() < 2) return false; else if (str.substring(0,2).equals(str.substring(str.length()-2, str.length()))) return true; else return false; }
Thaks for nice logic
ReplyDeleteThaks for nice logic
ReplyDeleteThank you
ReplyDeleteThere is a solution
public boolean frontAgain(String str) {
return str.length()>=2 && str.substring(0, 2).equals(str.substring(str.length()-2, str.length()));
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()>1 && str.charAt(0) == str.charAt(str.length()-2)
&& str.charAt(1) == str.charAt(str.length()-1))
return true;
else return false;
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()>1 && str.substring(0,2).equals(str.substring(str.length()-2)))
return true;
else return false;
}
public boolean frontAgain(String str) {
ReplyDeleteif (str.length() < 2) return false;
if (str.endsWith(str.substring(0, 2))) return true;
else return false;
}
public boolean frontAgain(String str) {
ReplyDeleteif (str.length() < 2) return false;
if (str.endsWith(str.substring(0, 2))) return true;
else return false;
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()<2){
return false;
}
String first2 = str.substring(0,2);
return str.startsWith(first2) && str.endsWith(first2);
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()>1&&str.endsWith(str.substring(0,2)))
return true;
return false;
}
public boolean frontAgain(String str) {
ReplyDeletereturn (str.length()>1&&str.endsWith(str.substring(0,2)));
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()>1){
String a=str.substring(0,2);
String b=str.substring(str.length()-2,str.length());
if(a.equals(b)){
return true;
}else return false;
}else return false;
}
public boolean frontAgain(String str) {
ReplyDeleteif(str.length()<2){
return false;
}
return str.substring(0,2).equals(str.substring(str.length()-2));
}
public boolean frontAgain(String str) {
ReplyDeleteif (str.length()>1 && str.endsWith(str.substring(0,2))) {
return true;
}
return false;
}
public boolean frontAgain(String str) {
ReplyDeletereturn (str.length()>=2 && (str.substring(0,2).equals(str.substring(str.length()-2))));
}