Java Program to print distinct permutations of a string
Given a string str, the task is to print all the distinct 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 = “abbb”
Output: [abbb, babb, bbab, bbba]
Input: str = “abc”
Output: [abc, bac, bca, acb, cab, cba]
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. Before adding the generated string, just check if it has already been generated before to get the distinct permutations.
Below is the implementation of the above approach:
// Java implementation of the approach import java.util.ArrayList; public class GFG { // Function that returns true if string s // is present in the Arraylist static boolean isPresent(String s, ArrayList<String> Res) { // If present then return true for (String str : Res) { if (str.equals(s)) return true ; } // Not present return false ; } // Function to return an ArrayList containg // all the distinct permutations of the string static ArrayList<String> distinctPermute(String str) { // If string is empty if (str.length() == 0 ) { // Return an empty arraylist ArrayList<String> baseRes = new ArrayList<>(); baseRes.add( "" ); return baseRes; } // Take first character of str char ch = str.charAt( 0 ); // Rest of the string after excluding // the first character String restStr = str.substring( 1 ); // Recurvise call ArrayList<String> prevRes = distinctPermute(restStr); // Store the generated sequence into // the resultant Arraylist ArrayList<String> Res = new ArrayList<>(); for (String s : prevRes) { for ( int i = 0 ; i <= s.length(); i++) { String f = s.substring( 0 , i) + ch + s.substring(i); // If the generated string is not // already present in the Arraylist if (!isPresent(f, Res)) // Add the generated string to the Arraylist Res.add(f); } } // Return the resultant arraylist return Res; } // Driver code public static void main(String[] args) { String s = "abbb" ; System.out.println(distinctPermute(s)); } } |
[abbb, babb, bbab, bbba]
Optimization : We can optimize above solution to use HashSet to store result strings in place of Res ArrayList.
Recommended Posts:
- Java Program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print first n distinct permutations of string using itertools in Python
- Iterative program to generate distinct Permutations of a String
- Distinct permutations of a string containing duplicates using HashSet in Java
- Write a program to print all permutations of a given string
- Print all permutations of a string in Java
- Print all the permutations of a string without repetition using Collections in Java
- Distinct permutations of the string | Set 2
- Print distinct sorted permutations with duplicates allowed in input
- Print all palindrome permutations of a string
- Print all the palindromic permutations of given string in alphabetic order
- Print all distinct characters of a string in order (3 Methods)
- Find distinct characters in distinct substrings of a string
- Java Program for Anagram Substring Search (Or Search for all permutations)
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.