Problem:
Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.
extraEnd("Hello") → "lololo"
extraEnd("ab") → "ababab"
extraEnd("Hi") → "HiHiHi"
Solution:
public String extraEnd(String str) { String s = str.substring(str.length()-2, str.length()); return s+s+s; }
public String extraEnd(String str) {
ReplyDeleteif(str.length()<2)
{
return str;
}
String end = str.substring(str.length()-2,str.length());
return end+end+end;
}
public String extraEnd(String str) {
ReplyDeletefinal String lastTwo = "" + str.charAt(str.length() - 2) + str.charAt(str.length() - 1);
return lastTwo + lastTwo + lastTwo ;
}
public String extraEnd(String str) {
ReplyDeleteString res="";
String m=str.substring(str.length()-2,str.length());
if(str.length()==2)
return res=str+str+str;
else
return res=m+m+m;
}
public String extraEnd(String str) {
ReplyDeleteString penultimate = str.substring(str.length() - 2);
String last = str.substring(str.length());
String sumReiteration = penultimate + last;
return sumReiteration + sumReiteration + sumReiteration;
}
public String extraEnd(String str) {
ReplyDeleteif(str.length()-1<1)
{
return str + str + str;
}
else
{
String newstr=str.substring(str.length()-2,str.length());
return newstr + newstr + newstr;
}
}
public String extraEnd(String str) {
ReplyDeleteString ans ;
int s2 = str.length();
String s1 = str.substring(s2-2,s2);
ans = s1+s1+s1;
return ans;
}