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”
We have discussed C implementation to print all permutations of a given string using backtracking here. In this post, C++ implementation using STL is discussed.
Method 1 (Using rotate())
std::rotate function rotates elements of a vector/string such that the passed middle element becomes first. For example, if we call rotate for “ABCD” with middle as second element, the string becomes “BCDA” and if we again call rotate with middle as second element, the string becomes “CDAB”. Refer this for a sample program.
Below is C++ implementation.
ABC ACB BCA BAC CAB CBA
Method 2 (using next_permutation)
We can use next_permutation that modifies a string so that it stores lexicographically next permutation. If current string is lexicographically largest, i.e., “CBA”, then next_permutation returns false.
We first sort the string, so that it is converted to lexicographically smallest permutation. Then we one by one call next_permutation until it returns false.
ABC ACB BAC BCA CAB CBA
Note that the second method always prints permutations in lexicographically sorted order irrespective of input string.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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
- All permutations of an array using STL in C++
- All reverse permutations of an array using STL in C++
- Find Kth largest string from the permutations of the string with two characters
- All permutations of a string using iteration
- Print first n distinct permutations of string using itertools in Python
- Distinct permutations of a string containing duplicates using HashSet in Java
- Write a program to print all permutations of a given string
- Print all distinct permutations of a given string with duplicates
- Print all the palindromic permutations of given string in alphabetic order
- Check if given string can be formed by two other strings or their permutations
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Print all lexicographical greater permutations of a given string
- Count of distinct permutations of every possible length of given string
- Generate all permutations of a string that follow given constraints
- Maximize count of corresponding same elements in given permutations using cyclic rotations
- Minimize count of given operations required to make two given strings permutations of each other
- Count possible permutations of given array satisfying the given conditions
- Print all palindrome permutations of a string
- Time complexity of all permutations of a string