Write a Java Program to Compute all the permutations of the string

Permutations are a fundamental concept in computer science and mathematics.

They represent all possible rearrangements of a set of elements.

In the case of a string, the permutations represent all possible rearrangements of its characters.

In this tutorial, we will explore how to compute all permutations of a string in Java.


Firstly, let us understand the concept of permutation.

A permutation is a way of arranging a set of objects in a specific order.

For example, the permutations of the set {1,2,3} are {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2}, and {3,2,1}. As we can see, the order of the elements in each permutation is different.

In Java, we can compute all permutations of a string using recursion.

The idea is to swap each character of the string with every other character and then recursively compute the permutations of the remaining substring.

We can do this by iterating over the string and swapping each character with the first character, then recursively computing the permutations of the remaining substring, and finally swapping the characters back to their original positions.

Let us see how to implement this in Java.

public class Permutations {
    public static void main(String[] args) {
        String str = "abc";
        int n = str.length();
        permute(str, 0, n-1);
    }

    private static void permute(String str, int l, int r) {
        if (l == r)
            System.out.println(str);
        else {
            for (int i = l; i <= r; i++) {
                str = swap(str,l,i);
                permute(str, l+1, r);
                str = swap(str,l,i);
            }
        }
    }

    private static String swap(String a, int i, int j) {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }
}

In the above code, we have defined a permute() method that takes a string str and two indices l and r.

The l and r indices represent the starting and ending indices of the substring for which we want to compute the permutations.

If l and r are equal, it means we have reached the end of the substring, and we can print the current permutation.

Otherwise, we swap each character of the string with every other character and recursively call the permute() method on the remaining substring.

We also have a swap() method that takes a string a and two indices i and j.

This method swaps the characters at positions i and j of the string.

Finally, in the main() method, we initialize the input string str and call the permute() method with the starting and ending indices of the string.

When we run the program, we get the following output:

abc
acb
bac
bca
cba
cab

As we can see, the program has computed all possible permutations of the input string “abc”.


In conclusion, we have seen how to compute all permutations of a string in Java using recursion.

The idea is to swap each character with every other character and recursively compute the permutations of the remaining substring.

We hope this tutorial has been helpful in understanding the concept of permutations and how to compute them in Java.