CodingBat-- coding recursively

1.9k views Asked by At

I am trying the coding bat problem repeatFront:

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"

Here is the code I am trying:

public String repeatFront(String str, int n) {
    if(n==0) {
        return str;
    }
    sub = str.substring(0,n);
    sub = sub+repeatFront(sub,n-1);
    return sub;
}

The error I get is that there is an extra character at the end of my string. The first example would give "ChocChoChCC" and the second "ChoChCC" and so on. I just want to know, conceptually, what I am doing wrong and how to fix it.

7

There are 7 answers

0
River On BEST ANSWER

Ah I found your problem.

You must only return the empty string if n == 0.

Returning str will return that extra last letter a second time as the call repeatFront(Ch, 1) and repeatFront(C, 0) both return C.

Fix by changing the return on n==0 to return ""; :

if(n==0) {
    return "";
}
1
Ouney On

Well this works -

 public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    repeatFront("Chocolate", 4,sb);
    System.out.println(sb);
}

public static void repeatFront(String str, int n,StringBuilder sb) {
    if(n==0) {
        return;
    }
    sb.append(str.substring(0,n));
    repeatFront(str,n-1,sb);
}
}

Issues - for n=0, you are returning sub because of which you are getting last extra character. I have used StringBuilder which has made it clean and also got rid of overhead of creating extra string objects for each call.

0
M.zar On

you can use this:

public String repeatEnd(String str, int n) {
  if (n == 0)
  return "";

  String res = re(str, n);
  String resl ="";

  for (int i =0 ; i < n ; i ++){
    resl = resl + res;
  }
  return resl;
}
public String re(String s , int n){
  String end = "";

  int len = s.length();

  end = s.substring(len-n , len);

  return end;
}
0
nabster On

You can also use Java 11 String.repeat()

public String repeatEnd(String str, int n) {
    return str.substring(str.length() - n).repeat(n);
}
0
Anil kumar On
public String repeatFront(String str, int n) {
  //take a empty string
  String temp = "";
  //check if it greater then zero value of n
  while(n>0)
  {
//run a for loop and add the string data in temp string
  for(int i=0;i<=n;i++)
  {
    temp = temp + str.substring(0,n);
//decrement the n value after every for loop
    n--;
  }
  }
//now return the temp string
  return temp;
}
0
candy On

you can use this one too.

public String repeatFront(String str, int n){

    int i;
    String result = "";
    for(i=n; 0<i; i--){
        result += str.substring(0,i);
    }
    return result;
}
0
priyanshu kumar On
    public String repeatFront(String str, int n) {
    String result="";
   if(str.length()==0){
   return "";
   }
   if(str.length()>=1){
     for(int i=n;i>=1;i--){enter code here
       String sub = str.substring(0,i);
       result = result + sub;
     }

   return result;
 }