Permutations of a given string using STL
Given a string s, the task is to return all unique permutations of a given string in lexicographically sorted order.
Note: A permutation is the rearrangement of all the elements of a string.
Examples:
Input: s = "ABC"
Output: "ABC", "ACB", "BAC", "BCA", "CBA", "CAB"Input: s = "XY"
Output: "XY", "YX"Input: s = "AAA"
Output: "AAA"
Note: An approach using backtracking has already been discussed in Distinct Permutation of String. In this article two approaches using STL has been discussed.
Using Rotate() - O(n * n!) Time and O(n) Space
The idea is to generate all possible permutation of given string s using the rotate() function which rotates elements of string such that the passed middle element becomes first. To do so create an auxiliary string x and a HashSetres to store unique permutation in sorted order.
Start iterating from the first element and for each recursive call check if string s is empty, if so store the string x in res else recur again by removing the first element of string s and adding to last of string x. At last rotate the string s byremoving the first character from string s and appending it to the lastto generate the next permutation.
Note: For C++, rotate() stl is used, while for other languages, same operation has been done by removing the first character from string s and appending it to the last.
Below is given the implementation.
// C++ Program to find permutation
// of string using rotate() function
#include <bits/stdc++.h>
using namespace std;
// Recursive Function to generate permutation
void recurPermute(string s, string x, set<string> &res) {
// if string s becomes empty, then
// we have permutation stored in x
if (s.size() == 0) {
// store the permutation in res
res.insert(x);
return;
}
// else iterate through the string
for (int i = 0; i < s.size(); i++) {
// remove the first character from
// string s and add to string x
recurPermute(s.substr(1), x + s[0], res);
// rotating such that second element becomes first
rotate(s.begin(), s.begin() + 1, s.end());
}
}
// Function to find all permutation of string
vector<string> findPermutation(string &s){
// to store all the permutations
set<string> res;
// call the recursive function
recurPermute(s, "", res);
// stores all unique permutation
vector<string> ans;
for(auto i:res){
ans.push_back(i);
}
return ans;
}
int main() {
string s = "ABC";
vector<string> ans = findPermutation(s);
for(auto i: ans){
cout << i << " ";
}
return 0;
}
Output
ABC ACB BAC BCA CAB CBA
Time Complexity: O(n*n!)
Auxiliary Space: O(n)
Using next_permutation() Function - O(n * n!) Time and O(1) Space
C++ provides an in-built function called next_permutation(), that return directly lexicographically in the next greater permutation of the input. The idea is to firstly sort the string s and then call this function until all the permutations are not generated.
Note: Python also have a function permutations() similar to c++.For other programming languages similar function is available in different libraries which can be used to implement same.
Below is given the implementation:
// C++ program to find permutations
// of string using next_permutation
#include <bits/stdc++.h>
using namespace std;
// Function to find all permutation of string
vector<string> findPermutation(string &s){
// sort the string s
sort(s.begin(), s.end());
// stores all unique permutation
vector<string> ans;
// iterate until the next permutation exist
do {
ans.push_back(s);
} while (next_permutation(s.begin(), s.end()));
return ans;
}
int main() {
string s = "ABC";
vector<string> ans = findPermutation(s);
for(auto i: ans){
cout << i << " ";
}
return 0;
}
# Python program to find unique permutations
# of string using itertools.permutations
from itertools import permutations
# Function to find all unique permutations of a string
def findPermutation(s):
# Generate all unique permutations
s = ''.join(sorted(s))
ans = sorted(set(''.join(p) for p in permutations(s)))
return ans
# Main function
if __name__ == "__main__":
s = "ABC"
ans = findPermutation(s)
print(" ".join(ans))
Output
ABC ACB BAC BCA CAB CBA
Time Complexity: O(n*n!)
Auxiliary Space: O(1)