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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java Program to print distinct permutations of a string
- Print all permutations of a string in Java
- Print all palindrome permutations of a string
- Print all distinct permutations of a given string with duplicates
- Print all lexicographical greater permutations of a given string
- Print first n distinct permutations of string using itertools in Python
- Print all the permutations of a string without repetition using Collections in Java
- Print all the palindromic permutations of given string in alphabetic order
- Iterative program to generate distinct Permutations of a String
- Write a program to reverse an array or string
- Program to print all substrings of a given string
- Print the two possible permutations from a given sequence
- Java program to print all duplicate characters in a string
- C program to print the length of a String using %n format specifier
- Java program to print Even length words in a String
- Print k different sorted permutations of a given array
- Print all permutations with repetition of characters
- Print all permutations of a number N greater than itself
- Find Kth largest string from the permutations of the string with two characters
- Program to print characters present at prime indexes in a given string