Problem:
Given a string and an int N, return a string made of N repetitions of the last N characters of the string. You may assume that N is between 0 and the length of the string, inclusive.
repeatEnd("Hello", 3) → "llollollo"
repeatEnd("Hello", 2) → "lolo"
repeatEnd("Hello", 1) → "o"
Solution:
public String repeatEnd(String str, int n) { int len = str.length(); String newWord = ""; for (int i = 0; i < n; i++) { newWord += str.substring(len - n, len); } return newWord; }
public String repeatEnd(String str, int n) {
ReplyDeleteString result = "";
for(int i=0; i<str.length() && i<n; i++)
{
result+= str.substring(str.length()-n);
}
return result;
}
yes
Deletepublic String repeatEnd(String str, int n) {
ReplyDeleteint len = str.length();
String str1 = "";
for (int i = 0; i < n ; i++)
{
str1 = str1+ str.substring(len-n, len);
}
return str1;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString needed = str.substring(str.length()-n);
String result = "";
for (int i = 0; i < n; i++) {
result += needed;
}
return result;
public String repeatEnd(String str, int n) {
ReplyDeleteString s1="";
String s2="";
int size=n;
for(int i=0;i<n;i++)
{
s1=s1+str.charAt(str.length()-size);
size--;
}
for(int j=0;j<n;j++)
{
s2=s1+s2;
}
return s2;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString result = "";
if ( n == 0 || n <= str.length() ){
for ( int i = 0; i < n ; i++){
result += str.substring(str.length() - n );
}
}
return result;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString sol;
if(n==0)
{
return "";
}
else if(n==3)
{
sol=str.substring(str.length()-3)+str.substring(str.length()-3)+
str.substring(str.length()-3);
return sol;
}
else if(n==2)
{
sol=str.substring(str.length()-2)+str.substring(str.length()-2);
return sol;
}
return str.substring(str.length()-1);
}
String str2="";
ReplyDeletefor(int j=0;j<n;j++)
{
str2+=str.substring(str.length()-n,str.length());
}
return str2;
public String repeatEnd(String str, int n) {
ReplyDeleteString newWord = "";
for(int i = 0; i < n; i++){
newWord = newWord + str.substring(str.length()-n);
}
return newWord;
}
String d="";
ReplyDeletefor(int i=0;i<n;i++){
d+=str.substring(str.length()-n);
}
return d;
}
public String repeatEnd(String str, int n) {
ReplyDeleteif(n == 0 ){
return "";
}
return new String(new char[n]).replace("\0", str.substring(str.length()-n));
}
public String repeatEnd(String str, int n) {
ReplyDeletereturn (n == 0 )? ""
:new String(new char[n]).replace("\0", str.substring(str.length()-n));
}
public String repeatEnd(String str, int n) {
ReplyDeleteString blank = "";
String tail = str.substring(str.length()-n);
for(int i=0; i<n; i++){
blank += tail;
}
return blank;
}
Laura lassoon pyaaz mirchi!
ReplyDeletepublic String repeatEnd(String str, int n) {
String blank = "";
String end = str.substring(str.length() - n);
for(int i=0; i<n; i++){
blank = blank + end;
}return blank;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString repeat = "";
for (int i = 0; i < str.length(); i++) {
if (i >= str.length() - n) {
repeat = repeat + str.substring(str.length() - n);
}
}
return repeat;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString res = "";
int len = str.length()-n;
for(int i = 0; i < n; ++i) {
res += str.substring(len);
}
return res;
}
public String repeatEnd(String str, int n)
ReplyDelete{
String s = "";
for(int i = 0; i < n; i++)
{
s += str.substring(str.length()-n,str.length());
}
return s;
}
public String repeatEnd(String str, int n) {
ReplyDeleteString result = "";
int i = n;
while(i != 0)
{
result += str.substring(str.length()-n, str.length());
i--;
}
return result;
}
Another way
ReplyDeletepublic String repeatEnd(String str, int n) {
return String.join("", Collections.nCopies(n, str.substring(str.length() - n)));
}