Problem:
Given a string and an int n, return a string made of the first n characters of the string, followed by the first n-1 characters of the string, and so on. You may assume that n is between 0 and the length of the string, inclusive (i.e. n >= 0 and n <= str.length()).
repeatFront("Chocolate", 4) → "ChocChoChC"
repeatFront("Chocolate", 3) → "ChoChC"
repeatFront("Ice Cream", 2) → "IcI"
Solution:
public String repeatFront(String str, int n) { int len = str.length(); String newWord = ""; for (int i = n; n > 0; n--){ newWord += str.substring(0,n); } return newWord; }
String result = "";
ReplyDeletefor( ; n >= 0; n-- ) {
result += str.substring(0, n);
}
return result;
String s="";
ReplyDeletefor(int i=0;i<n;)
{
s = s + str.substring(i,n);
n--;
}
return s;
String result = "";
ReplyDeletewhile (n > 0) {
result = result + str.substring(0,n);
n--;
}
return result;
public String repeatFront(String str, int n) {
ReplyDeleteString result="";
int a =n;
for(int i=0;i<a;i++){
result+=str.substring(0,n);
n--;
}
return result;
}
public String repeatFront(String str, int n) {
ReplyDeleteString c= "";
for(int i=n;i>=0;i--) {
c+=str.substring(0,i);
}
return c;
}
public String repeatFront(String str, int n) {
ReplyDeleteString result = "";
if ( n == 0 || n <= str.length() ){
for ( int i = n; i > 0 ; i--){
result += str.substring(0,i);
}
}
return result;
}
public String repeatFront(String str, int n) {
ReplyDeleteint len=str.length();
String s="";
for(n=n; n>=0; n--){
s += str.substring(0,n);
}
return s;
}
public String repeatFront(String str, int n) {
ReplyDeleteif(n==4)
{
str=str.substring(0,4)+str.substring(0,3)+str.substring(0,2)+str.substring(0,1);
return str;
}
else if(n==3)
{
str=str.substring(0,3)+str.substring(0,2)+str.substring(0,1);
return str;
}
else if(n==2)
{
return str.substring(0,2)+str.substring(0,1);
}
else if(n==0)
{
return "";
}
else if(n==1)
{
return str.substring(0,1);
}
return "";
}
String str2="";
ReplyDeletefor(int i=n;i>=0;i--)
{
str2+=str.substring(0,i);
}
return str2;
public String repeatFront(String str, int n) {
ReplyDeleteif (n < 1)
{
return "";
}
else
{
return str.substring(0, n) + repeatFront(str.substring(0, n - 1), n - 1);
}
}
public String repeatFront(String str, int n) {
ReplyDeleteif (n == 0 || str.isEmpty()){
return "";
}
return str.substring(0, n) + repeatFront(str, n - 1);
}
public String repeatFront(String str, int n) {
ReplyDeletereturn (n == 0 || str.isEmpty())? "":
str.substring(0, n) + repeatFront(str, n - 1);
}
Kya boltay bantai log!!!
ReplyDeletexD
Deletepublic String repeatFront(String str, int n) {
ReplyDeleteString res="";
while(n>0){
res+=str.substring(0,n);
n--;
}
return res;
}
public String repeatFront(String str, int n)
ReplyDelete{
String s = "";
for(int i = 0; i < n; n--) s += str.substring(0 , n);
return s;
}
bravo
ReplyDeletepublic String repeatFront(String str, int n) {
ReplyDeleteStringBuilder newStr = new StringBuilder();
int count = n;
for(int i = 0; i < n; i++){
newStr.append(str.substring(0,count--));
}
return newStr.toString();
}
public String repeatFront(String str, int n) {
ReplyDeleteString sum="";
str=str.substring(0,n);
for(int i=0; i<str.length(); i++){
String temp=str.substring(0,str.length()-i);
sum+=temp;
}
return sum;
}
public String repeatFront(String str, int n) {
ReplyDeleteString newstr = str.substring(0, n);
int j = 0;
String k = "";
for (int i = newstr.length(); i >= 0; i--) {
for (int l = i; l > 0; l--) {
k += newstr.charAt(j);
j++;
}
j = 0;
}
return k;
}