Print all permutation of a string using ArrayList
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.
Examples:
Input: str = “abc”
Output: abc acb bac bca cba cabInput: 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:
Java
// Java implementation of the approach import java.util.ArrayList; public class GFG { // Utility function to print the contents // of the ArrayList static void printArrayList(ArrayList<String> arrL) { arrL.remove(""); for (int i = 0; i < arrL.size(); i++) System.out.print(arrL.get(i) + " "); } // Function to returns the arraylist which contains // all the permutation of str public static ArrayList<String> getPermutation(String str) { // If string is empty if (str.length() == 0) { // Return an empty arraylist ArrayList<String> empty = new ArrayList<>(); empty.add(""); return empty; } // Take first character of str char ch = str.charAt(0); // Take sub-string starting from the // second character String subStr = str.substring(1); // Recurvise call ArrayList<String> prevResult = getPermutation(subStr); // Store the generated permutations // into the resultant arraylist ArrayList<String> Res = new ArrayList<>(); for (String val : prevResult) { for (int i = 0; i <= val.length(); i++) { Res.add(val.substring(0, i) + ch + val.substring(i)); } } // Return the resultant arraylist return Res; } // Driver code public static void main(String[] args) { String str = "abc"; printArrayList(getPermutation(str)); } } |
C#
// C# implementation of the approach using System.Collections.Generic; using System; class GFG { // Utility function to print the contents // of the ArrayList static void printArrayList(List<String> arrL) { arrL.Remove(""); for (int i = 0; i < arrL.Count; i++) Console.Write(arrL[i] + " "); } // Function to returns the arraylist which contains // all the permutation of str public static List<String> getPermutation(String str) { // If string is empty if (str.Length == 0) { // Return an empty arraylist List<String> empty = new List<String>(); empty.Add(""); return empty; } // Take first character of str char ch = str[0]; // Take sub-string starting from the // second character String subStr = str.Substring(1); // Recurvise call List<String> prevResult = getPermutation(subStr); // Store the generated permutations // into the resultant arraylist List<String> Res = new List<String>(); foreach (String val in prevResult) { for (int i = 0; i <= val.Length; i++) { Res.Add(val.Substring(0, i) + ch + val.Substring(i)); } } // Return the resultant arraylist return Res; } // Driver code public static void Main(String[] args) { String str = "abc"; printArrayList(getPermutation(str)); } } // This code has been contributed by 29AjayKumar |
abc bac bca acb cab cba
Recommended Posts:
- Print all subsequences of a string using ArrayList
- Convert an ArrayList of String to a String array in Java
- Java ArrayList to print all possible words from phone digits
- Minimum length of string having all permutation of given string.
- Lexicographically n-th permutation of a string
- Find n-th lexicographically permutation of a string | Set 2
- Number of distinct permutation a String can have
- String which when repeated exactly K times gives a permutation of S
- Lexicographically smallest permutation of a string with given subsequences
- Maximum even length sub-string that is permutation of a palindrome
- Print shortest path to print a string on screen
- Print all possible ways to convert one string into another string | Edit-Distance
- Print the longest prefix of the given string which is also the suffix of the same string
- Permutation of a string with maximum number of characters greater than its adjacent characters
- ArrayList of 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 contribute@geeksforgeeks.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



