Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String permutation with recursion

I am a java beginner and trying to do a string permutation practice from java programming book. I am defining two method:

public static void displayPermutation(String s)
public static void displayPermutation(String s1, String s2)

The first method simply invokes displayPermutation(" ", s). The second method uses a loop to move a character from s2 to s1 and recursively invokes it with a new s1 and s2. The base case is that s2 is empty and prints s1 to the console.

Can anyone help me to find what is the problem of the following code?

Her's example:

 public static void displayPermutation(String s) {
            displayPermuatation("", s);
        }

    private static void displayPermuatation(String s1, String s2) {
        //base case: when s2 is empty, print s1
        if (s2.isEmpty()) {
            System.out.println(s1);
        }
        else {          
        for (int i = 0; i < s2.length(); i++) {
                        //move a char from s1 to s2, and recursively invokes it with 
                        //new s1 and s2
            s1 = s1 + s2.charAt(i);
            s2 = s2.substring(0, i) + s2.substring(i+1);
            displayPermuatation(s1, s2);
        }
    }
   }

if s = "abc", it prints only: abc acb

it seems that in the first call of displayPermuatation("", "abc"), it does not finish the for loop.... any comments?

Thanks for all the comments below. I think the mistakes I made is because that passing object as argument to a method is actually passing the reference. it is not like primitive data (passing by value). When changing the object, it will affect following method call using that object.

like image 989
Vortex Avatar asked Jan 21 '14 15:01

Vortex


People also ask

How do you find the recursion of a permutation of a string?

Method 1(Using Recursion) :Create a recursive function say permute(string s, int l, int r), and pass string along with starting index of the string and the ending index of the string. Base condition will be if(l==r) then print the s. Otherwise, run a loop from [l, r] And, swap(s[l], s[i])

What is string permutation?

A Permutation of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are Permutation of each other.

How do you generate all permutations of a string in Python?

To find all possible permutations of a given string, you can use the itertools module which has a useful method called permutations(iterable[, r]). This method return successive r length permutations of elements in the iterable as tuples.


2 Answers

Do not alter s1 and s2 in the loop, that causes the error. Simply pass those definitions as arguments to recursive function. Like this:

.
.
for (int i = 0; i < s2.length(); i++) {
        displayPermuatation(s1 + s2.charAt(i), s2.substring(0, i) + s2.substring(i+1));
    }
.
.
like image 160
Mehmet Sedat Güngör Avatar answered Oct 17 '22 19:10

Mehmet Sedat Güngör


Problem with your code is that you are changing value of s1 and s2 in the loop which affects the following iterations in the loop, see the following code where I have fixed this issue.

public static void displayPermutation(String s) {
    displayPermuatation("", s);
}

private static void displayPermuatation(String s1, String s2) {
    // base case: when s2 is empty, print s1
    if (s2.isEmpty()) {
        System.out.println(s1);
    } else {
        for (int i = 0; i < s2.length(); i++) {
            // move a char from s1 to s2, and recursively invokes it with
            // new s1 and s2
            displayPermuatation(s1 + s2.charAt(i), s2.substring(0, i) + s2.substring(i + 1));
        }
    }
}
like image 23
Hemant Avatar answered Oct 17 '22 18:10

Hemant