Write a Program in Java to print all permutations of a string.

Example:

The plan is to make use of recursion to solve this problem because every substring is itself a string.

Example:

Also if the string contains duplicate alphabets then there is a sure chance that the same permutation value will be printed more than one time, Eg lol, lol. To check this we will store each already printed permutations into a list and whenever we form a new permutation we first check if that is already contained in the list or not and will only output it if it is not there in the list.

So let’s print all permutation of the string in Java.

Output

We can also input number to print all its permutation in the above program because it will be treated as a string. All permutations of a string can also be said as anagrams of a string, so the above program is also the program for all anagrams of a string.

Leave a Reply

Close Menu