Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. A permutation should not have repeated strings in the output.
Input: str = “aa”
Note that “aa” will be printed only once
as duplicates are not allowed.
Input: str = “ab”
Approach: Write a recursive function that removes a character one by one from the original string and generates a new string by appending these removed characters. The base condition will be when all the characters have been used. In that case, insert the generated string (a permutation of the original string) in a set in order to avoid duplicates.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Print all permutations of a string in Java
- Java Program to print distinct permutations of a string
- Write a program to print all permutations of a given string
- Generate all permutations of a string that follow given constraints
- Print all subsequences of a string
- Print all permutation of a string using ArrayList
- Given a string, print all possible palindromic partitions
- Print all palindromic partitions of a string
- Print all subsequences of a string using ArrayList
- Print all the combinations of a string in lexicographical order
- Java ArrayList to print all possible words from phone digits
- Check if a given string is sum-string
- Print a pattern without using any loop
- Print all possible paths from the first row to the last row in a 2D array
- Print sums of all subsets of a given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.