1

I am new to recursion and backtracking. How the code after the recursive call is being executed?

 class Solution {

// str : Stores input string
// n : Length of str.
// curr : Stores current permutation
// index : Index in current permutation, curr
static void permuteRec(String str, int n,
                       int index, String curr)
{
    // base case
    if (index == n) {
        return;
    }
    System.out.println(curr);
    for (int i = index + 1; i < n; i++) {

        curr += str.charAt(i);
        permuteRec(str, n, i, curr);

        // backtracking
        curr = curr.substring(0, curr.length() - 1);
    }
    return;
}

// Generates power set in lexicographic
// order.
static void powerSet(String str)
{
    char[] arr = str.toCharArray();
    Arrays.sort(arr);
    permuteRec(new String(arr), str.length(), -1, "");
}

// Driver code
public static void main(String[] args)
{
    String str = "abc";
    powerSet(str);
}
}

I cannot understand how the backtracking is going on inside the for loop?

The output is:

a ab b c ca cab cb 
Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • What don't you understand, specifically? What is your background knowledge--do you have any experience with recursion? – ggorlen Jul 23 '22 at 15:19
  • @ggorlen I cannot understand how the code is working after the recursive call. – Bodhisattwa Basu Jul 24 '22 at 04:11
  • do you understand what `curr += str.charAt(i);` is doing? – Will Ness Jul 25 '22 at 16:45
  • do you understand what `curr = curr.substring(0, curr.length() - 1);` is doing, and how it relates to the above action? – Will Ness Jul 25 '22 at 17:42
  • @WillNess I don't undertand (`curr = curr.substring(0, curr.length() - 1);`) – Bodhisattwa Basu Jul 26 '22 at 13:07
  • @WillNess I just want to know how the backtracking is happening inside the for loop – Bodhisattwa Basu Jul 26 '22 at 14:02
  • to understand whole you need to understand each of its parts first. I asked you two questions, you answered only one. To try to help you I need to know what you know and what you you don't know. also, is this Java? – Will Ness Jul 26 '22 at 16:33
  • also, how do you call this function? please give a specific example, like, `permuteRec( "123", 3, 0, "")`, is this it? – Will Ness Jul 26 '22 at 16:36
  • @WillNess yes ,this is java. No ,I don't know what the first one is doing as well. – Bodhisattwa Basu Jul 26 '22 at 16:38
  • in general, the `for` loop _is_ the backtracking. the loop sets `i` (etc.), loop body is executed, control returns to the loop, which then _tries the next value for `i`_ (etc.), i.e. executes the loop body with the _next_ value for `i` (etc.). – Will Ness Jul 26 '22 at 16:39
  • OK, `curr += str.charAt(i);` for the specific example of `curr = ""`, `i=0` and `str = "123"`, means `curr += "123".charAt(0)` i.e. `curr += '1'` so it changes `curr` by adding `'1'` at the end, so `curr` becomes `"1"`. (I don't know Java, if its strings are indexed from 1 and not from 0, then replace 0 in the above with 1). – Will Ness Jul 26 '22 at 16:43
  • in your main, using `"cab"` is just confusing. `"abc"` is simpler to follow. I've edited. – Will Ness Jul 26 '22 at 16:47
  • @WillNess the string is indexed from 0 – Bodhisattwa Basu Jul 26 '22 at 16:48
  • OK, so can you follow the code for the call `powerSet("abc")`? it becomes something like `permuteRec( "abc", 3, -1, "")`, right? – Will Ness Jul 26 '22 at 16:50
  • I forgot to tell you, `curr = curr.substring(0, curr.length() - 1);` removes the last character from `curr`: if it was say `"abc"`, then after that statement `curr` becomes `"ab"`. – Will Ness Jul 27 '22 at 03:32

0 Answers0