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.
For instance, the words ‘bat’ and ‘tab’ represents two distinct permutation (or arrangements) of a similar three letter word.
Input: str = “abc”
Output: abc acb bac bca cba cab
Input: str = “bat”
Output: bat bta abt atb tba tab
Approach: Write a recursive function that will generate all the permutations of the string. Terminating condition will be when the passed string is empty, in that case the function will return an empty ArrayList.
Below is the implementation of the above approach:
abc bac bca acb cab cba
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- ArrayList of ArrayList in Java
- Print all subsequences of a string using ArrayList
- Java ArrayList to print all possible words from phone digits
- Convert an ArrayList of String to a String array in Java
- Minimum length of string having all permutation of given string.
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Remove all elements from the ArrayList in Java
- How to add all items from a collection to an ArrayList in Java?
- Print the last character of lexicographically smallest non-palindromic permutation of a string
- Graph Representation using Java ArrayList
- Check if any permutation of string is a K times repeated string
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Find the N-th lexicographic permutation of string using Factoradic method
- Array vs ArrayList in Java
- ArrayList in Java
- ArrayList vs LinkedList in Java
- How to remove an element from ArrayList in Java?
- ArrayList to Array Conversion in Java : toArray() Methods
- ArrayList and LinkedList remove() methods in Java with Examples
- Vector vs ArrayList in Java
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.
Improved By : 29AjayKumar