1

I have written an algorithm for solving string permutations algorithm. It's having a problem, somewhere it's printing wrong output. I have walked through my code multiple times for different inputs, but I am unable to find what mistake I have done.

Would someone explain where I have made a mistake?

public static void main(String args[]) throws Exception {

    String str1 = "eat";
    int len = str1.length();
    char[] str = str1.toCharArray();
    recurPerm(str,0,len);
}
static void recurPerm(char[] str, int strstartind, int sz) {

    for(int i = strstartind; i < sz; i++){
        if(i > strstartind) {

            char temp = str[strstartind];
            str[strstartind] = str[i];
            str[i] = temp;

            recurPerm(str, strstartind + 1, sz);
        }
        else {
            recurPerm(str, strstartind + 1, sz);
        }
    }
    if(strstartind >= sz){
        System.out.println(str);
    }

    return;
}

Output

eat
eta
tea 
tae
eat
eta

But correct output is

eat eta aet ate tae tea

I tried debugging too. I found it too hard to debug because recursion being involved.

1
  • 4
    @NiVeR The output missing aet and ate. Commented Jul 24, 2018 at 10:50

1 Answer 1

3

You should restore the original state of the recursion, otherwise you keep mixing up the given parameters. The function parameter "strstartind" only indicates the right char to pick, if the original "str[]" is not changed, however you overwrite the original "str[]" at:

    str[strstartind] = str[i];
    str[i] = temp;

here is a working solution, that restores the recursion parameters:

public class DemoApplicationTests {

public static void main(String args[]) throws Exception {

    String str1 = "eat";
    int len = str1.length();
    char[] str = str1.toCharArray();
    recurPerm(str, 0, len);
}

static void recurPerm(char[] str, int strstartind, int sz) {

    for (int i = strstartind; i < sz; i++) {
        char temp = str[strstartind];
        str[strstartind] = str[i];
        str[i] = temp;
        recurPerm(str, strstartind + 1, sz);
        //restore state
        str[i] = str[strstartind];
        str[strstartind] = temp;
    }
    if (strstartind >= sz) {
        System.out.println(str);
    }

    return;
}
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanxx. It works well.I understood your point, but Can you please explain what was mistake in my approach by taking example "eat" string where this not restoring approach being facing problem.
"Not restoring approach" was a general problem in your approach. It has nothing to do with the example you choose.
I am aware of it. But I am asking you take as example that "eat" string and explain where the problem has arised.
@Lukas-Franken edited my answer by adding additional description
@Lukas-Franken I am changing my original string. Thats fine but those are happening in different function calls. they shouldn't effect my functions local variable str right?? thats what i am asking
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.