>>> from itertools import permutations
>>> perms = [''.join(p) for p in permutations('stack')]
>>> perms
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]);
}
}
}
# get all permutations of string
import itertools
for p in itertools.permutations('123'):
print(p) # ( ' 1 ', ' 2 ', ' 3 ') ( ' 1 ' , ' 3 ', ' 2 ' ) ( ' 2 ', ' 1 ', ' 3 ' )
void permutation(string s)
{
sort(s.begin(),s.end());
do{
cout << s << " ";
}
while(next_permutation(s.begin(),s.end()); // std::next_permutation
cout << endl;
}