Print all distinct permutations of a given string with duplicates
Given a string that may contain duplicates, write a function to print all permutations of given string such that no permutation is repeated in output.
Examples:
Input: str[] = "AB" Output: AB BA Input: str[] = "AA" Output: AA Input: str[] = "ABC" Output: ABC ACB BAC BCA CBA CAB Input: str[] = "ABA" Output: ABA AAB BAA Input: str[] = "ABCA" Output: AABC AACB ABAC ABCA ACBA ACAB BAAC BACA BCAA CABA CAAB CBAA
We have discussed an algorithm to print all permutations in below post. It is strongly recommended to refer below post as a prerequisite of this post.
Write a C program to print all permutations of a given string
The algorithm discussed on above link doesn’t handle duplicates.
CPP
// Program to print all permutations of a // string in sorted order. #include <bits/stdc++.h> using namespace std; // This function finds the index of the smallest character // which is greater than 'first' and is present in str[l..h] int findCeil(string str, char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the elements and find the // smallest character greater than 'first' for ( int i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order void sortedPermutations(string str) { // Get size of string int size = str.length(); // Sort the string in increasing order sort(str.begin(), str.end()); // Print permutations one by one bool isFinished = false ; while (!isFinished) { // print this permutation cout << str << endl; // printf("%d %s \n", x++, str); // Find the rightmost character which is smaller // than its next character. Let us call it 'first // char' int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break ; // If there is no such character, all are sorted in // decreasing order, means we just printed the last // permutation and we are done. if (i == -1) isFinished = true ; else { // Find the ceil of 'first char' in right of // first character. Ceil of a character is the // smallest character greater than it int ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters swap(str[i], str[ceilIndex]); // Sort the string on right of 'first char' sort(str.begin() + i + 1, str.end()); } } } // Driver program to test above function int main() { string str = "ACBC" ; sortedPermutations(str); return 0; } // This code is contributed by Aditya Kumar (adityakumar129) |
C
// Program to print all permutations of a string in sorted // order. #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* Following function is needed for library function qsort(). */ int compare( const void * a, const void * b) { return (*( char *)a - *( char *)b); } // A utility function two swap two characters // a and b void swap( char * a, char * b) { char t = *a; *a = *b; *b = t; } // This function finds the index of the smallest character // which is greater than 'first' and is present in str[l..h] int findCeil( char str[], char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the elements and find the // smallest character greater than 'first' for ( int i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order void sortedPermutations( char str[]) { // Get size of string int size = strlen (str); // Sort the string in increasing order qsort (str, size, sizeof (str[0]), compare); // Print permutations one by one bool isFinished = false ; while (!isFinished) { // print this permutation static int x = 1; printf ( "%d %s \n" , x++, str); // Find the rightmost character which is smaller // than its next character. Let us call it 'first // char' int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break ; // If there is no such character, all are sorted in // decreasing order, means we just printed the last // permutation and we are done. if (i == -1) isFinished = true ; else { // Find the ceil of 'first char' in right of // first character. Ceil of a character is the // smallest character greater than it int ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters swap(&str[i], &str[ceilIndex]); // Sort the string on right of 'first char' qsort (str + i + 1, size - i - 1, sizeof (str[0]), compare); } } } // Driver program to test above function int main() { char str[] = "ACBC" ; sortedPermutations(str); return 0; } // This code is contributed by Aditya Kumar (adityakumar129) |
Java
// Java Program to print all permutations of a // string in sorted order. import java.util.*; class GFG { // This function finds the index of the smallest // character which is greater than 'first' and is // present in str[l..h] static int findCeil( char [] str, char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the elements and find // the smallest character greater than 'first' for ( int i = l + 1 ; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order static void sortedPermutations(String str1) { // Get size of string int size = str1.length(); char [] str = str1.toCharArray(); // Sort the string in increasing order Arrays.sort(str); // Print permutations one by one boolean isFinished = false ; int x = 1 ; while (!isFinished) { // print this permutation System.out.println(x++ + " " + new String(str)); // printf("%d %s \n", x++, str); // Find the rightmost character which is smaller // than its next character. Let us call it // 'first char' int i; for (i = size - 2 ; i >= 0 ; --i) if (str[i] < str[i + 1 ]) break ; // If there is no such character, all are sorted // in decreasing order, means we just printed // the last permutation and we are done. if (i == - 1 ) isFinished = true ; else { // Find the ceil of 'first char' in right of // first character. Ceil of a character is // the smallest character greater than it int ceilIndex = findCeil(str, str[i], i + 1 , size - 1 ); // Swap first and second characters char temp = str[i]; str[i] = str[ceilIndex]; str[ceilIndex] = temp; // Sort the string on right of 'first char' Arrays.sort(str, i + 1 , str.length ); } } } // Driver program to test above function public static void main(String[] args) { String str = "ACBC" ; sortedPermutations(str); } } // This code is contributed by phasing17 |
Python3
# Program to print all permutations of a # string in sorted order. # This function finds the index of the smallest character # which is greater than 'first' and is present in str[l..h] def findCeil(str1, first, l, h): # initialize index of ceiling element ceilIndex = l # Now iterate through rest of the elements and find the # smallest character greater than 'first' for i in range (l + 1 , 1 + h): if (str1[i] > first and str1[i] < str1[ceilIndex]): ceilIndex = i return ceilIndex # Print all permutations of str in sorted order def sortedPermutations(str1): size = len (str1) str1 = list (str1) # Sort the string in increasing order str1.sort() # Print permutations one by one isFinished = False x = 1 while ( not isFinished): # print this permutation print (x, "".join(str1)) x + = 1 # Find the rightmost character which is smaller # than its next character. Let us call it 'first # char' i = len (str1) - 2 while i > = 0 : if (str1[i] < str1[i + 1 ]): break i - = 1 # If there is no such character, all are sorted in # decreasing order, means we just printed the last # permutation and we are done. if (i = = - 1 ): isFinished = True else : # Find the ceil of 'first char' in right of # first character. Ceil of a character is the # smallest character greater than it ceilIndex = findCeil(str1, str1[i], i + 1 , size - 1 ) # Swap first and second characters temp = str1[i] str1[i] = str1[ceilIndex] str1[ceilIndex] = temp # Sort the string on right of 'first char' str1 = str1[ 0 : i + 1 ] + sorted (str1[i + 1 :]) # Driver program to test above function str1 = "ACBC" sortedPermutations(str1) # This code is contributed by phasing17 |
C#
// C# Program to print all permutations of a // string in sorted order. using System; using System.Collections.Generic; class GFG { // This function finds the index of the smallest // character which is greater than 'first' and is // present in str[l..h] static int findCeil( char [] str, char first, int l, int h) { // initialize index of ceiling element int ceilIndex = l; // Now iterate through rest of the elements and find // the smallest character greater than 'first' for ( int i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order static void sortedPermutations( string str1) { // Get size of string int size = str1.Length; char [] str = str1.ToCharArray(); // Sort the string in increasing order Array.Sort(str); // Print permutations one by one bool isFinished = false ; int x = 1; while (!isFinished) { // print this permutation Console.WriteLine(x++ + " " + new string (str)); // printf("%d %s \n", x++, str); // Find the rightmost character which is smaller // than its next character. Let us call it // 'first char' int i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break ; // If there is no such character, all are sorted // in decreasing order, means we just printed // the last permutation and we are done. if (i == -1) isFinished = true ; else { // Find the ceil of 'first char' in right of // first character. Ceil of a character is // the smallest character greater than it int ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters char temp = str[i]; str[i] = str[ceilIndex]; str[ceilIndex] = temp; // Sort the string on right of 'first char' Array.Sort(str, i + 1, str.Length - i - 1); } } } // Driver program to test above function public static void Main( string [] args) { string str = "ACBC" ; sortedPermutations(str); } } // This code is contributed by phasing17 |
Javascript
// Program to print all permutations of a // string in sorted order. // This function finds the index of the smallest character // which is greater than 'first' and is present in str[l..h] function findCeil(str, first, l, h) { // initialize index of ceiling element let ceilIndex = l; // Now iterate through rest of the elements and find the // smallest character greater than 'first' for (let i = l + 1; i <= h; i++) if (str[i] > first && str[i] < str[ceilIndex]) ceilIndex = i; return ceilIndex; } // Print all permutations of str in sorted order function sortedPermutations(str) { // Get size of string let size = str.length; str = str.split( "" ); // Sort the string in increasing order str.sort(); // Print permutations one by one let isFinished = false ; let x = 1; while (!isFinished) { // print this permutation console.log(x++ + " " + str.join( "" )); // printf("%d %s \n", x++, str); // Find the rightmost character which is smaller // than its next character. Let us call it 'first // char' let i; for (i = size - 2; i >= 0; --i) if (str[i] < str[i + 1]) break ; // If there is no such character, all are sorted in // decreasing order, means we just printed the last // permutation and we are done. if (i == -1) isFinished = true ; else { // Find the ceil of 'first char' in right of // first character. Ceil of a character is the // smallest character greater than it let ceilIndex = findCeil(str, str[i], i + 1, size - 1); // Swap first and second characters let temp = str[i]; str[i] = str[ceilIndex]; str[ceilIndex] = temp; // Sort the string on right of 'first char' str = str.slice(0, i + 1).concat(str.slice(i + 1).sort()); } } } // Driver program to test above function let str = "ACBC" ; sortedPermutations(str); // This code is contributed by phasing17 |
ABCC ACBC ACCB BACC BCAC BCCA CABC CACB CBAC CBCA CCAB CCBA
The above code is taken from a comment below by Mr. Lazy.
Time Complexity: O(n2 * n!)
Auxiliary Space: O(1)
The above algorithm is in the time complexity of O(n2 * n!) but we can achieve a better time complexity of O(n!*n) which was there in the case of all distinct characters in the input by some modification in that algorithm. The distinct characters algorithm can be found here – https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
Efficient Approach: In our recursive function to find all permutations, we can use unordered_set for taking care of duplicate element remaining in the active string. While iterating over the elements of the string, we will check for that element in the unordered_set and if it found then we will skip that iteration or otherwise we will insert that element into unordered_set. As on an average all the unordered_set operations like insert() and find() are in O(1) time then the algorithm time complexity will not change by using unordered_set.
The algorithm implementation is as follows –
C++
#include <bits/stdc++.h> using namespace std; void printAllPermutationsFast(string s, string l) { if (s.length() < 1) cout << l + s << endl; unordered_set< char > uset; for ( int i = 0; i < s.length(); i++) { if (uset.find(s[i]) != uset.end()) continue ; else uset.insert(s[i]); string temp = "" ; if (i < s.length() - 1) temp = s.substr(0, i) + s.substr(i + 1); else temp = s.substr(0, i); printAllPermutationsFast(temp, l + s[i]); } } int main() { string s = "ACBC" ; sort(s.begin(), s.end()); printAllPermutationsFast(s, "" ); return 0; } // This code is contributed by Aditya Kumar (adityakumar129) |
Java
// Java code to implement the approach import java.util.*; class GFG { // Method to print all the permutations static void printAllPermutationsFast(String s, String l) { if (s.length() < 1 ) System.out.println(l + s); // Set of the characters of the permutation HashSet<Character> uset = new HashSet<Character>(); // Iterating over the string characters for ( int i = 0 ; i < s.length(); i++) { // If this permutation has already been // created, form the next permutation if (uset.contains(s.charAt(i))) continue ; else // Otherwise, update the set uset.add(s.charAt(i)); // Create the permutation that contains the ith // character and the permutation that does not // contain the ith character String temp = "" ; if (i < s.length() - 1 ) temp = s.substring( 0 , i) + s.substring(i + 1 ); else temp = s.substring( 0 , i); printAllPermutationsFast(temp, l + s.charAt(i)); } } // Driver method public static void main(String[] args) { String s = "ACBC" ; // Sorting the string // using char array char [] arr = s.toCharArray(); Arrays.sort(arr); s = new String(arr); // Function call printAllPermutationsFast(s, "" ); } } // This code is contributed by phasing17 |
Python3
# Python3 code for the same approach def printAllPermutationsFast(s, l): if ( len (s) < 1 ): print (l + s) uset = set () for i in range ( len (s)): if s[i] in uset: continue else : uset.add(s[i]) temp = ""; if (i < len (s) - 1 ): temp = s[:i] + s[i + 1 :] else : temp = s[:i] printAllPermutationsFast(temp, l + s[i]) # Driver code s = "ACBC" ; s = "".join( sorted (s)) printAllPermutationsFast(s, "") # This code is contributed by phasing17 |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Method to print all the permutations static void printAllPermutationsFast( string s, string l) { if (s.Length < 1) Console.WriteLine(l + s); // Set of the characters of the permutation HashSet< char > uset = new HashSet< char >(); // Iterating over the string characters for ( int i = 0; i < s.Length; i++) { // If this permutation has already been // created, form the next permutation if (uset.Contains(s[i])) continue ; else // Otherwise, update the set uset.Add(s[i]); // Create the permutation that contains the ith character // and the permutation that does not contain the ith // character string temp = "" ; if (i < s.Length - 1) temp = s.Substring(0, i) + s.Substring(i + 1); else temp = s.Substring(0, i); printAllPermutationsFast(temp, l + s[i]); } } // Driver method public static void Main( string [] args) { string s = "ACBC" ; // Sorting the string // using char array char [] arr = s.ToCharArray(); Array.Sort(arr); s = new string (arr); // Function call printAllPermutationsFast(s, "" ); } } // This code is contributed by phasing17 |
Javascript
<script> // JavaScript code for the same approach function printAllPermutationsFast(s, l) { if (s.length < 1) { document.write(l + s, "</br>" ); } let uset = new Set(); for (let i = 0; i < s.length; i++) { if (uset.has(s[i]) == true ) { continue ; } else { uset.add(s[i]); } let temp = "" ; if (i < s.length - 1) { temp = s.substring(0, i) + s.substring(i + 1); } else { temp = s.substring(0, i); } printAllPermutationsFast(temp, l + s[i]); } } // driver code let s = "ACBC" ; s = s.split( "" ).sort().join( "" ); printAllPermutationsFast(s, "" ); // This code is contributed by shinjanpatra. </script> |
ABCC ACBC ACCB BACC BCAC BCCA CABC CACB CBAC CBCA CCAB CCBA
Time Complexity – O(n*n!)
Auxiliary Space – O(n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Please Login to comment...