Write a Python Program to Find Permutation of a Given String

In this tutorial, we will write the Python program to find a permutation of the given string. The problem is a given string S, we need to find all unique permutations of the given string in lexicographically sorted order. Below is an example -

Example - 1:

Example - 2:

Solution

We will solve this problem using the recursive backtracking approach. Let's understand the following code snippet.

Example -

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation -

In the above code, we define a unique_permutations() function takes this string, converts it into a list of characters, sorts them in lexicographic order, and then uses a recursive backtracking approach to generate all unique permutations. The permutations are stored in the result list and returned as the final output.

Method - 2:

In this method, we will use the itertools library and set data structure.

Example -

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation -

In this implementation, the itertools.permutations function is used to generate all possible permutations of the given string. The permutations are then passed to the set function to eliminate duplicates, and finally sorted to ensure lexicographic order. Each unique permutation is printed using the print statement.

Method - 3:

In this method, we utilizes a combination of itertools and functools libraries to solve the problem. Let's understand the following example.

Example -

Output -

In this solution, the unique_permutations() function uses the itertools.permutations() function to generate all possible permutations of the given string. The permutations are then sorted using the sorted function and a custom comparison function compare_permutations(). The compare_permutations() function compares two permutations by concatenating them in different orders and comparing the resulting strings lexicographically.

The functools.cmp_to_key() function is used to convert the comparison function to a key function suitable for sorting. Finally, the sorted permutations are converted back to strings and returned as the result.

Method - 4:

To get all unique permutations of a given string in lexicographically sorted order, we will use a recursive approach with memoization. This approach can help avoid redundant computations and improve efficiency. Let's understand the following example.

Example -

Output:

ABC
ACB
BAC
BCA
CAB
CBA

Explanation -

In this implementation, the unique_permutations() function takes the string and uses a recursive function generate_permutations() to generate all unique permutations. The function recursively fixes each character at the beginning and generates permutations for the remaining characters. Memoization is used to store already computed permutations to avoid redundant computations.

The computed permutations are then sorted in lexicographic order using the sorted function. Finally, the sorted permutations are returned as the result.

This method with memoization offers an alternative solution that reduces computation time by avoiding duplicate calculations.






Latest Courses