Problem:
Given a string of any length, return a new string where the last 2 chars, if present, are swapped, so "coding" yields "codign".
lastTwo("coding") → "codign"
lastTwo("cat") → "cta"
lastTwo("ab") → "ba"
Solution:
public String lastTwo(String str) { if (str.length() < 2) return str; else return str.substring(0, str.length()-2) + str.charAt(str.length()-1) + str.charAt(str.length()-2); }
The blog helps me to to improve my knowledge in the latest technology. Thank you admin
ReplyDeleteSimple Truth
Article submission sites
public String lastTwo(String str) {
ReplyDeletereturn str.length()>1?str.substring(0,str.length()-2)+str.substring(str.length()-1)+str.substring(str.length()-2,str.length()-2+1):str;
}
public String lastTwo(String str) {
ReplyDeleteif(str.length()>=2){
String a=str.substring(0,str.length()-2);
String b=str.substring(str.length()-2,str.length()-1);
String c=str.substring(str.length()-1,str.length());
return a+c+b;
}
if(str.length()<2){
return str;
}return str;
}
public String lastTwo(String str) {
ReplyDeletewhile (str.length() >= 2){
String a = str.substring(0, str.length() - 2);
String b = str.substring(str.length() - 2, str.length() - 1);
String c = str.substring(str.length() - 1, str.length());
return a + c + b;
}
return str;
}
function lastTwo(str){
ReplyDeletelet beforeLastChar = str.substring(str.length - 1, str.length - 2);
let lastChar = str.substring(str.length - 1);
str = str.substring(0, str.length - 2)
return str + lastChar + beforeLastChar;
}
MMORPG OYUNLAR
ReplyDeleteinstagram takipçi satın al
Tiktok jeton hilesi
TİKTOK JETON HİLESİ
Saç ekim antalya
Takipci
İnstagram takipçi satın al
mt2 pvp serverler
İnstagram Takipci Satın Al