A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB
Here is a solution that is used as a basis in backtracking.
ABC ACB BAC BCA CBA CAB
Algorithm Paradigm: Backtracking
Time Complexity: O(n*n!) Note that there are n! permutations and it requires O(n) time to print a a permutation.
Note : The above solution prints duplicate permutations if there are repeating characters in input string. Please see below link for a solution that prints only distinct permutations even if there are duplicates in input.
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
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.
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Print all distinct permutations of a given string with duplicates
- Print all the palindromic permutations of given string in alphabetic order
- Print all lexicographical greater permutations of a given string
- Print all palindrome permutations of a string
- Print all permutations of a string in Java
- Print all the permutations of a string without repetition using Collections in Java
- Java Program to print distinct permutations of a string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Generate all permutations of a string that follow given constraints
- Print all permutations with repetition of characters
- Print all permutations in sorted (lexicographic) order
- Iterative approach to print all permutations of an Array
- Print all permutations of a number N greater than itself
- Print first n distinct permutations of string using itertools in Python
- Print the two possible permutations from a given sequence
- Print k different sorted permutations of a given array
- Print all possible ways to write N as sum of two or more positive integers
- All permutations of a string using iteration
- Time complexity of all permutations of a string