C++ Program To Print All Permutations Of A Given String Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report 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. Source: Mathword(http://mathworld.wolfram.com/Permutation.html) Below are the permutations of string ABC. ABC ACB BAC BCA CBA CAB Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Here is a solution that is used as a basis in backtracking. C++ // C++ program to print all permutations // with duplicates allowed #include <bits/stdc++.h> using namespace std; // Function to print permutations // of string // This function takes three parameters: // 1. String // 2. Starting index of the string // 3. Ending index of the string. void permute(string a, int l, int r) { // Base case if (l == r) cout<<a<<endl; else { // Permutations made for (int i = l; i <= r; i++) { // Swapping done swap(a[l], a[i]); // Recursion called permute(a, l+1, r); //backtrack swap(a[l], a[i]); } } } // Driver Code int main() { string str = "ABC"; int n = str.size(); permute(str, 0, n-1); return 0; } // This is code is contributed by rathbhupendra Output: 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 permutation. Auxiliary Space: O(r - l) Note: The above solution prints duplicate permutations if there are repeating characters in the input string. Please see the below link for a solution that prints only distinct permutations even if there are duplicates in input.Print all distinct permutations of a given string with duplicates. Permutations of a given string using STL Another approach: C++ // C++ program to implement // the above approach #include <bits/stdc++.h> #include <string> using namespace std; void permute(string s, string answer) { if(s.length() == 0) { cout << answer << " "; return; } for(int i = 0; i < s.length(); i++) { char ch = s[i]; string left_substr = s.substr(0, i); string right_substr = s.substr(i + 1); string rest = left_substr + right_substr; permute(rest , answer+ch); } } // Driver code int main() { string s; string answer = ""; cout << "Enter the string : "; cin >> s; cout << "All possible strings are : "; permute(s, answer); return 0; } Output: Enter the string : abc All possible strings are : abc acb bac bca cab cba Time Complexity: O(n*n!) The time complexity is the same as the above approach, i.e. there are n! permutations and it requires O(n) time to print a permutation. Auxiliary Space: O(|s|) Please refer complete article on Write a program to print all permutations of a given string for more details! Comment More infoAdvertise with us Next Article C++ Program To Print All Permutations Of A Given String K kartik Follow Improve Article Tags : Strings Greedy Backtracking Mathematical Combinatorial Recursion C++ Programs DSA Amazon Samsung Walmart Cisco Citrix Snapdeal Accolite MAQ Software OYO permutation Apple +15 More Practice Tags : AccoliteAmazonAppleCiscoCitrixMAQ SoftwareSamsungSnapdealWalmartBacktrackingCombinatorialGreedyMathematicalpermutationRecursionStrings +12 More Similar Reads Iterative program to generate distinct Permutations of a String Given a string str, the task is to generate all the distinct permutations of the given string iteratively. Examples: Input: str = "bba" Output: abb bab bba Input: str = "abc" Output: abc acb bac bca cab cba Approach: The number of permutations for a string of length n are n!. The following algorithm 15+ min read All reverse permutations of an array using STL in C++ Given an array, the task is to print or display all the reverse permutations of this array using STL in C++. Reverse permutation means, for an array {1, 2, 3}: forward permutations: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 reverse permutations: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 Examples: Input: a[] = { 3 min read C++ Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"} Output : 2 Explanation: In first string, we remove first element("m") from first 3 min read Find All Permutations of an Array using STL in C++ The permutation of an array refers to a rearrangement of its elements in every possible order. In this article, we will learn how to generate all possible permutation of an array using STL in C++.The simplest method to find all the permutations of an array is to use next_permutation(). The array has 2 min read C++ Program for Check if given string can be formed by two other strings or their permutations Given a string str and an array of strings arr[], the task is to check if the given string can be formed by any of the string pair from the array or their permutations. Examples: Input: str = "amazon", arr[] = {"loa", "azo", "ft", "amn", "lka"} Output: Yes The chosen strings are "amn" and "azo" whic 4 min read Like