49
void permute(string elems, int mid, int end)
{
    static int count;
    if (mid == end) {
        cout << ++count << " : " << elems << endl;
        return ;
    }
    else {
    for (int i = mid; i <= end; i++) {
            swap(elems, mid, i);
            permute(elems, mid + 1, end);
            swap(elems, mid, i);
        }
    }
}

The above function shows the permutations of str(with str[0..mid-1] as a steady prefix, and str[mid..end] as a permutable suffix). So we can use permute(str, 0, str.size() - 1) to show all the permutations of one string.

But the function uses a recursive algorithm; maybe its performance could be improved?

Are there any better methods to permute a string?

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
Jichao
  • 40,341
  • 47
  • 125
  • 198
  • 4
    Have you looked at the STL's next_permutation function? http://www.cplusplus.com/reference/algorithm/next_permutation/ – Mark Byers Jan 03 '10 at 15:51
  • 3
    Not sure what are you looking for? We have functions for permutations in both STL and Boost, Is it that you aren't happy with their performance or is it that you are interested in the implementation. – Narendra N Jan 08 '10 at 16:46
  • Now that I put all that work into an answer I hope someone notices before the bounty expires, even if it's to tell me what a horrible hack it is. :-) – Omnifarious Jan 15 '10 at 04:17
  • Added the explanation you asked for. – Omnifarious Jan 15 '10 at 20:53

20 Answers20

64

Here is a non-recursive algorithm in C++ from the Wikipedia entry for unordered generation of permutations. For the string s of length n, for any k from 0 to n! - 1 inclusive, the following modifies s to provide a unique permutation (that is, different from those generated for any other k value on that range). To generate all permutations, run it for all n! k values on the original value of s.

#include <algorithm>

void permutation(int k, string &s) 
{
    for(int j = 1; j < s.size(); ++j) 
    {
        std::swap(s[k % (j + 1)], s[j]); 
        k = k / (j + 1);
    }
}

Here swap(s, i, j) swaps position i and j of the string s.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
Permaquid
  • 1,950
  • 1
  • 14
  • 15
  • 3
    Someone selected the answer that said your answer was the best one. *sigh* And your answer is the best one. – Omnifarious Jan 15 '10 at 16:50
  • 4
    Such is life. The best-laid plans o mice and men gang aft agley. – Permaquid Jan 21 '10 at 03:56
  • 5
    It's been four year since and Wikipedia article has been changed now so can you please elaborate why this would work! Exactly why it is guaranteed to be a kth unique permutation? – Harshdeep Feb 09 '14 at 08:57
  • @Harshdeep I guess https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order is where it is explained ... – Kedar Mhaswade Jul 08 '16 at 02:28
52

Why dont you try std::next_permutation() or std::prev_permutation() ?

Links:

std::next_permutation()
std::prev_permutation()

A simple example:

#include<string>
#include<iostream>
#include<algorithm>

int main()
{
   std::string s="123";
   do
   {

      std::cout<<s<<std::endl;

   }while(std::next_permutation(s.begin(),s.end()));
}

Output:

123
132
213
231
312
321
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • 18
    keep in mind that in order to get all permutations your initial string/array must be sorted in ascending order. – Omry Yadan Jan 03 '10 at 16:28
  • 1
    I think the STL has to re-examine the sequence each time it is called. The code in the question doesn't have to do any comparisons, so I think that might be more efficient (plus, it would work even on types that don't support `<`). – Jason Orendorff Jan 04 '10 at 02:08
  • 2
    Omry: INCORRECT. It goes in a cycle. The next permutation of the greatest permutation is the least permutation. – Potatoswatter Jan 14 '10 at 00:52
  • Using the return `bool` return value as the condition, you're correct, though. – Potatoswatter Jan 14 '10 at 00:54
  • 1
    Remember, STL was invented by crazed mathematicians. Seriously, if you apply the algorithms correctly, you get high efficiency. And it's all part of C++! – Jon Reid Jan 15 '10 at 06:38
  • 1
    If the STL were really crazy math, it would have these: http://en.wikipedia.org/wiki/Fibonacci_heap – Potatoswatter Jan 15 '10 at 09:53
20

I'd like to second Permaquid's answer. The algorithm he cites works in a fundamentally different way from the various permutation enumeration algorithms that have been offered. It doesn't generate all of the permutations of n objects, it generates a distinct specific permutation, given an integer between 0 and n!-1. If you need only a specific permutation, it's much faster than enumerating them all and then selecting one.

Even if you do need all permutations, it provides options that a single permutation enumeration algorithm does not. I once wrote a brute-force cryptarithm cracker, that tried every possible assignment of letters to digits. For base-10 problems, it was adequate, since there are only 10! permutations to try. But for base-11 problems took a couple of minutes and base-12 problems took nearly an hour.

I replaced the permutation enumeration algorithm that I had been using with a simple i=0--to--N-1 for-loop, using the algorithm Permaquid cited. The result was only slightly slower. But then I split the integer range in quarters, and ran four for-loops simultaneously, each in a separate thread. On my quad-core processor, the resulting program ran nearly four times as fast.

Just as finding an individual permutation using the permutation enumeration algorithms is difficult, generating delineated subsets of the set of all permutations is also difficult. The algorithm that Permaquid cited makes both of these very easy

Community
  • 1
  • 1
Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
  • Another thought - the algorithm maps permutations to an integer between 0 and n!-1, which quickly overflows any reasonable integer size. If you need to work with larger permutations, you need an extended integer representation. In this case, a factoradic representation will serve you best. In a factoradic representation, instead of each digit representing a multiple of 10^k, each digit represents a multiple of k!. There is a direct algorithm for mapping a factoradic representation into a permutation. You can find details at http://en.wikipedia.org/wiki/Factoradic#Permutations – Jeff Dege Jan 18 '10 at 14:34
11

In particular, you want std::next_permutation.

void permute(string elems, int mid, int end)
{
  int count = 0;
  while(next_permutation(elems.begin()+mid, elems.end()))
    cout << << ++count << " : " << elems << endl;
}

... or something like that...

grepit
  • 21,260
  • 6
  • 105
  • 81
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
4

Any algorithm for generating permutations is going to run in polynomial time, because the number of permutations for characters within an n-length string is (n!). That said, there are some pretty simple in-place algorithms for generating permutations. Check out the Johnson-Trotter algorithm.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
JohnE
  • 349
  • 1
  • 3
4

The Knuth random shuffle algorithm is worth looking into.

// In-place shuffle of char array
void shuffle(char array[], int n)
{
    for ( ; n > 1; n--)
    {
        // Pick a random element to move to the end
        int k = rand() % n;  // 0 <= k <= n-1  

        // Simple swap of variables
        char tmp = array[k];
        array[k] = array[n-1];
        array[n-1] = tmp;
    }
}
David R Tribble
  • 11,918
  • 5
  • 42
  • 52
3

Any algorithm that makes use of or generates all permutations will take O(N!*N) time, O(N!) at the least to generate all permutations and O(N) to use the result, and that's really slow. Note that printing the string is also O(N) afaik.

In a second you can realistically only handle strings up to a maximum of 10 or 11 characters, no matter what method you use. Since 11!*11 = 439084800 iterations (doing this many in a second on most machines is pushing it) and 12!*12 = 5748019200 iterations. So even the fastest implementation would take about 30 to 60 seconds on 12 characters.

Factorial just grows too fast for you to hope to gain anything by writing a faster implementation, you'd at most gain one character. So I'd suggest Prasoon's recommendation. It's easy to code and it's quite fast. Though sticking with your code is completely fine as well.

I'd just recommend that you take care that you don't inadvertantly have extra characters in your string such as the null character. Since that will make your code a factor of N slower.

JPvdMerwe
  • 3,328
  • 3
  • 27
  • 32
1

Actually you can do it using Knuth shuffling algo!

// find all the permutations of a string
// using Knuth radnom shuffling algorithm!

#include <iostream>
#include <string>

template <typename T, class Func>
void permutation(T array, std::size_t N, Func func)
{
    func(array);
    for (std::size_t n = N-1; n > 0; --n)
    {
        for (std::size_t k = 0; k <= n; ++k)
        {
            if (array[k] == array[n]) continue;
            using std::swap;
            swap(array[k], array[n]);
            func(array);
        }
    }
}

int main()
{
    while (std::cin.good())
    {
        std::string str;
        std::cin >> str;
        permutation(str, str.length(), [](std::string const &s){ 
            std::cout << s << std::endl; });
    }
}
Reza Toghraee
  • 1,603
  • 1
  • 14
  • 21
  • I know this is a few years old - but this solution doesn't give you all of the permutations. which ya know - is a problem. – Antiokus Feb 19 '16 at 19:45
1

I've written a permutation algorithm recently. It uses a vector of type T (template) instead of a string, and it's not super-fast because it uses recursion and there's a lot of copying. But perhaps you can draw some inspiration for the code. You can find the code here.

StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • `concat` is just an inferior version of `v.insert(v.begin(), item)`. `GetPermutations` just does the same thing as OP's code, which is inferior to a loop with `std::next_permutation`. – Potatoswatter Jan 14 '10 at 03:08
  • I never claimed my solution to be superior :) That said, I don't see how my GetPermutations function is the same as the OP's code. – StackedCrooked Jan 14 '10 at 11:06
  • Each call partitions the string into a stable and a recursively permuted part. – Potatoswatter Jan 15 '10 at 09:52
1

The only way to significantly improve performance is to find a way to avoid iterating through all the permutations in the first place!

Permuting is an unavoidably slow operation (O(n!), or worse, depending on what you do with each permutation), unfortunately nothing you can do will change this fact.

Also, note that any modern compiler will flatten out your recursion when optimisations are enabled, so the (small) performance gains from hand-optimising are reduced even further.

James
  • 24,676
  • 13
  • 84
  • 130
1

Do you want to run through all the permutations, or count the number of permutations?

For the former, use std::next_permutation as suggested by others. Each permutation takes O(N) time (but less amortized time) and no memory except its callframe, vs O(N) time and O(N) memory for your recursive function. The whole process is O(N!) and you can't do better than this, as others said, because you can't get more than O(X) results from a program in less than O(X) time! Without a quantum computer, anyway.

For the latter, you just need to know how many unique elements are in the string.

big_int count_permutations( string s ) {
    big_int divisor = 1;
    sort( s.begin(), s.end() );
    for ( string::iterator pen = s.begin(); pen != s.end(); ) {
        size_t cnt = 0;
        char value = * pen;
        while ( pen != s.end() && * pen == value ) ++ cnt, ++ pen;
        divisor *= big_int::factorial( cnt );
    }
    return big_int::factorial( s.size() ) / divisor;
}

Speed is bounded by the operation of finding duplicate elements, which for chars can be done in O(N) time with a lookup table.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • Any constructive criticism, or an example of input for which it fails? – Potatoswatter Jan 15 '10 at 06:03
  • `while ( pen != s.end() && * pen == value ) ++ cnt` will cause infinite loop. – Jichao Jan 15 '10 at 06:19
  • ah, correct. By the way, do you want permutations of unique elements, (n!) total, as your algo returns, or unique permutations, as counted by this? – Potatoswatter Jan 15 '10 at 06:35
  • actually,I hanv't consider unique before,i assume elements of the input string are unique in my alg. – Jichao Jan 15 '10 at 07:01
  • notice there are many other problems in your algorithm.here is my version to count unique permutations:http://code.google.com/p/jcyangs-alg-trunk/source/browse/trunk/recur/permutation/count/MyCount.cc – Jichao Jan 15 '10 at 08:27
  • Other problems such as what? Add `++pen` after `++cnt`. You shouldn't use `double` as a bignum class, by the way. Hmm, looking at my code, the call to `sort` isn't O(N). Whatever. – Potatoswatter Jan 15 '10 at 09:03
  • have you tested your code?simple case `abc` would produce wrong answer or even crash the program. – Jichao Jan 15 '10 at 09:08
  • i haven't use `uint64` before and `` would bring protability problem.definitly,i would update the code later.`the call to sort isn't O(n)`,what do you mean by this? – Jichao Jan 15 '10 at 09:25
  • I hadn't tested it before, but now that you ask, yes, it appears to work correctly once you add `++cnt`. – Potatoswatter Jan 15 '10 at 09:28
  • `uint64` also isn't a bignum class. You can use whatever kind of number you want; in any case, be sure you don't overflow. `sort` doesn't run in O(N) time, it's O(N log N). – Potatoswatter Jan 15 '10 at 09:29
  • it didn't work.`while ( pen != s.end() && * pen == value ) ++ cnt, ++ pen; `,while pen iterate to the end of the string,cnt become 0,then divisor should be 0 too,which lead to the wrong result. – Jichao Jan 15 '10 at 10:12
  • antoher problem lies in your alg.Considering input 'aabbc',your alg would produce `5! / (2! * 2!)`,but the right answer should be `c(5,2) * c(3,2) * 1!`. – Jichao Jan 15 '10 at 10:16
  • 1. `cnt` will always be advanced at least once. 2: factorial(0) = 1 even if it weren't. 3. C(n,k) = n!/(k!(n-k)!) so C(5,2)*C(3,2) = 5!3!/(2!3!2!1!) = 5!/(2!2!). You can work through the algebra to see they're always the same. – Potatoswatter Jan 15 '10 at 17:34
  • correct.i'm wrong.i didn't use the standard factorial function.and your alg is much cleaner than mine.anyway thanks. – Jichao Jan 16 '10 at 00:59
  • Lol, thx, I didn't even think you could do that. Usually (on other people's questions I guess) the vote locks in after about a minute. – Potatoswatter Jan 16 '10 at 04:03
  • not exactly.after you editing twice,the lock will be cleaned. – Jichao Jan 16 '10 at 05:18
1

I don't think this is better, but it does work and does not use recursion:

#include <iostream>
#include <stdexcept>
#include <tr1/cstdint>

::std::uint64_t fact(unsigned int v)
{
   ::std::uint64_t output = 1;
   for (unsigned int i = 2; i <= v; ++i) {
      output *= i;
   }
   return output;
}

void permute(const ::std::string &s)
{
   using ::std::cout;
   using ::std::uint64_t;
   typedef ::std::string::size_type size_t;

   static unsigned int max_size = 20;  // 21! > 2^64

   const size_t strsize = s.size();

   if (strsize > max_size) {
      throw ::std::overflow_error("This function can only permute strings of size 20 or less.");
   } else if (strsize < 1) {
      return;
   } else if (strsize == 1) {
      cout << "0 : " << s << '\n';
   } else {
      const uint64_t num_perms = fact(s.size());
      // Go through each permutation one-by-one
      for (uint64_t perm = 0; perm < num_perms; ++perm) {
         // The indexes of the original characters in the new permutation
         size_t idxs[max_size];

         // The indexes of the original characters in the new permutation in
         // terms of the list remaining after the first n characters are pulled
         // out.
         size_t residuals[max_size];

         // We use div to pull our permutation number apart into a set of
         // indexes.  This holds what's left of the permutation number.
         uint64_t permleft = perm;

         // For a given permutation figure out which character from the original
         // goes in each slot in the new permutation.  We start assuming that
         // any character could go in any slot, then narrow it down to the
         // remaining characters with each step.
         for (unsigned int i = strsize; i > 0; permleft /= i, --i) {
            uint64_t taken_char = permleft % i;
            residuals[strsize - i] = taken_char;

            // Translate indexes in terms of the list of remaining characters
            // into indexes in terms of the original string.
            for (unsigned int o = (strsize - i); o > 0; --o) {
               if (taken_char >= residuals[o - 1]) {
                  ++taken_char;
               }
            }
            idxs[strsize - i] = taken_char;
         }
         cout << perm << " : ";
         for (unsigned int i = 0; i < strsize; ++i) {
            cout << s[idxs[i]];
         }
         cout << '\n';
      }
   }
}

The fun thing about this is that the only state it uses from permutation to permutation is the number of the permutation, the total number of permutations, and the original string. That means it can be easily encapsulated in an iterator or something like that without having to carefully preserve the exact correct state. It can even be a random access iterator.

Of course ::std::next_permutation stores the state in the relationships between elements, but that means it can't work on unordered things, and I would really wonder what it does if you have two equal things in the sequence. You can solve that by permuting indexes of course, but that adds slightly more complication.

Mine will work with any random access iterator range provided it's short enough. And if it isn't, you'll never get through all the permutations anyway.

The basic idea of this algorithm is that every permutation of N items can be enumerated. The total number is N! or fact(N). And any given permutation can be thought of as a mapping of source indices from the original sequence into a set of destination indices in the new sequence. Once you have an enumeration of all permutations the only thing left to do is map each permutation number into an actual permutation.

The first element in the permuted list can be any of the N elements from the original list. The second element can be any of the N - 1 remaining elements, and so on. The algorithm uses the % operator to pull apart the permutation number into a set of selections of this nature. First it modulo's the permutation number by N to get a number from [0,N). It discards the remainder by dividing by N, then it modulo's it by the size of the list - 1 to get a number from [0,N-1) and so on. That is what the for (i = loop is doing.

The second step is translating each number into an index into the original list. The first number is easy because it's just a straight index. The second number is an index into a list that contains every element but the one removed at the first index, and so on. That is what the for (o = loop is doing.

residuals is a list of indices into the successively smaller lists. idxs is a list of indices into the original list. There is a one-one mapping between values in residuals and idxs. They each represent the same value in different 'coordinate spaces'.

The answer pointed to by the answer you picked has the same basic idea, but has a much more elegant way of accomplishing the mapping than my rather literal and brute force method. That way will be slightly faster than my method, but they are both about the same speed and they both have the same advantage of random access into permutation space which makes a whole number of things easier, including (as the answer you picked pointed out) parallel algorithms.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
0

Even I found it difficult to understand that recursive version of the first time and it took me some time to search for a berre way.Better method to find (that I can think of) is to use the algorithm proposed by Narayana Pandita. The basic idea is:

  1. First sort the given string in no-decreasing order and then find the index of the first element from the end that is less than its next character lexicigraphically. Call this element index the 'firstIndex'.
  2. Now find the smallest character which is greater thn the element at the 'firstIndex'. Call this element index the 'ceilIndex'.
  3. Now swap the elements at 'firstIndex' and 'ceilIndex'.
  4. Reverse the part of the string starting from index 'firstIndex+1' to the end of the string.
  5. (Instead of point 4) You can also sort the part of the string from index 'firstIndex+1' to the end of the string.

Point 4 and 5 do the same thing but the time complexity in case of point 4 is O(n*n!) and that in case of point 5 is O(n^2*n!).

The above algorithm can even be applied to the case when we have duplicate characters in the string. :

The code for displaying all the permutation of a string :

#include <iostream>

using namespace std;

void swap(char *a, char *b)
{
    char tmp = *a;
    *a = *b;
    *b = tmp;
}


int partition(char arr[], int start, int end)
{
    int x = arr[end];
    int i = start - 1;
    for(int j = start; j <= end-1; j++)
    {
        if(arr[j] <= x)
        {
            i = i + 1;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i+1], &arr[end]);
    return i+1;
}

void quickSort(char arr[], int start, int end)
{
    if(start<end)
    {
        int q = partition(arr, start, end);
        quickSort(arr, start, q-1);
        quickSort(arr, q+1, end);
    }
}

int findCeilIndex(char *str, int firstIndex, int n)
{
    int ceilIndex;
    ceilIndex = firstIndex+1;

    for (int i = ceilIndex+1; i < n; i++)
    {
        if(str[i] >= str[firstIndex] && str[i] <= str[ceilIndex])
            ceilIndex = i;
    }
    return ceilIndex;
}

void reverse(char *str, int start, int end)
{
    while(start<=end)
    {
        char tmp = str[start];
        str[start] = str[end];
        str[end] = tmp;
        start++;
        end--;
    }
}

void permutate(char *str, int n)
{
    quickSort(str, 0, n-1);
    cout << str << endl;
    bool done = false;
    while(!done)
    {
        int firstIndex;
        for(firstIndex = n-2; firstIndex >=0; firstIndex--)
        {
            if(str[firstIndex] < str[firstIndex+1])
                break;
        }
        if(firstIndex<0)
            done = true;
        if(!done)
        {
            int ceilIndex;
            ceilIndex = findCeilIndex(str, firstIndex, n);
            swap(&str[firstIndex], &str[ceilIndex]);
            reverse(str, firstIndex+1, n-1);
            cout << str << endl;
        }
    }
}


int main()
{
    char str[] = "mmd";
    permutate(str, 3);
    return 0;
}
Lokesh Basu
  • 31
  • 2
  • 8
0

This post: http://cplusplus.co.il/2009/11/14/enumerating-permutations/ deals with permuting just about anything, not only strings. The post itself and the comments below are pretty informative and I wouldn't want to copy&paste..

rmn
  • 2,386
  • 1
  • 14
  • 21
0

Here's one I just rustled up!!

void permute(const char* str, int level=0, bool print=true) {

    if (print) std::cout << str << std::endl;

    char temp[30];
    for (int i = level; i<strlen(str); i++) {

        strcpy(temp, str);

        temp[level] = str[i];
        temp[i] = str[level];

        permute(temp, level+1, level!=i);
    }
}

int main() {
    permute("1234");

    return 0;
}
Rich
  • 682
  • 1
  • 6
  • 14
0

If you are interested in permutation generation I did a research paper on it a while back : http://www.oriontransfer.co.nz/research/permutation-generation

It comes complete with source code, and there are 5 or so different methods implemented.

ioquatix
  • 1,411
  • 17
  • 32
0

This is not the best logic, but then, i am a beginner. I'll be quite happy and obliged if anyone gives me suggestions on this code

#include<iostream.h>
#include<conio.h>
#include<string.h>
int c=1,j=1;


int fact(int p,int l)
{
int f=1;
for(j=1;j<=l;j++)
{
f=f*j;
if(f==p)
return 1;

}
return 0;
}


void rev(char *a,int q)
{
int l=strlen(a);
int m=l-q;
char t;
for(int x=m,y=0;x<q/2+m;x++,y++)
{
t=a[x];
a[x]=a[l-y-1];
a[l-y-1]=t;
}
c++;
cout<<a<<"  ";
}

int perm(char *a,int f,int cd)
{
if(c!=f)
{
int l=strlen(a);
rev(a,2);
cd++;
if(c==f)return 0;
if(cd*2==6)
{
for(int i=1;i<=c;i++)
{
if(fact(c/i,l)==1)
{
rev(a,j+1);
rev(a,2);
break;
}
}
cd=1;
}
rev(a,3);
perm(a,f,cd);
}
return 0;
}

void main()
{
clrscr();
char *a;
cout<<"\n\tEnter a Word";
cin>>a;
int f=1;

for(int o=1;o<=strlen(a);o++)
f=f*o;

perm(a,f,0);
getch();
}
0
**// Prints all permutation of a string**

    #include<bits/stdc++.h>
    using namespace std;


    void printPermutations(string input, string output){
        if(input.length() == 0){
            cout<<output <<endl;
            return;
        }

        for(int i=0; i<=output.length(); i++){
            printPermutations(input.substr(1),  output.substr(0,i) + input[0] + output.substr(i));
        }
    }

    int main(){
        string s = "ABC";
        printPermutations(s, "");
        return 0;
    }
0

Here yet another recursive function for string permutations:

void permute(string prefix, string suffix, vector<string> &res) {
    if (suffix.size() < 1) {
        res.push_back(prefix);
        return;
    }
    for (size_t i = 0; i < suffix.size(); i++) {
        permute(prefix + suffix[i], suffix.substr(0,i) + suffix.substr(i + 1), res);
    }
}


int main(){
    string str = "123";
    vector<string> res;
    permute("", str, res);
}

The function collects all permutations in vector res. The idea can be generalized for different type of containers using templates and iterators:

template <typename Cont1_t, typename Cont2_t>
void permute(typename Cont1_t prefix,
    typename Cont1_t::iterator beg, typename Cont1_t::iterator end,
    Cont2_t &result)
{
    if (beg == end) {
        result.insert(result.end(), prefix);
        return;
    }
    for (auto it = beg; it != end; ++it) {
        prefix.insert(prefix.end(), *it);
        Cont1_t tmp;
        for (auto i = beg; i != end; ++i)
            if (i != it)
                tmp.insert(tmp.end(), *i);

        permute(prefix, tmp.begin(), tmp.end(), result);
        prefix.erase(std::prev(prefix.end()));
    }
}

int main()
{
    string str = "123";
    vector<string> rStr;
    permute<string, vector<string>>("", str.begin(), str.end(), rStr);

    vector<int>vint = { 1,2,3 };
    vector<vector<int>> rInt;
    permute<vector<int>, vector<vector<int>>>({}, vint.begin(), vint.end(), rInt);

    list<long> ll = { 1,2,3 };
    vector<list<long>> vlist;
    permute<list<long>, vector<list<long>>>({}, ll.begin(), ll.end(), vlist);
}

This may be an interesting programming exercise, but in production code you should use a non recusrive version of permutation , like next_permutation.

Andrushenko Alexander
  • 1,839
  • 19
  • 14
-1
  //***************anagrams**************//


  //************************************** this code works only when there are no   
  repeatations in the original string*************//
  #include<iostream>
  using namespace std;

  int counter=0;

  void print(char empty[],int size)
  {

  for(int i=0;i<size;i++)
  {
    cout<<empty[i];
  }
  cout<<endl;
  }


  void makecombination(char original[],char empty[],char comb[],int k,int& nc,int size)
{
nc=0;

int flag=0;
for(int i=0;i<size;i++)
{
    flag=0;                                                                   // {
    for(int j=0;j<k;j++)
    {
        if(empty[j]==original[i])                                                                // remove this code fragment
        {                                                                                        // to print permutations with repeatation
            flag=1;
            break;
        }
    }
    if(flag==0)                                                                // }
    {
        comb[nc++]=original[i];
    }
}
//cout<<"checks  ";
//    print(comb,nc);
}


void recurse(char original[],char empty[],int k,int size)
{
char *comb=new char[size];


int nc;


if(k==size)
{
    counter++;
    print(empty,size);
    //cout<<counter<<endl;

}
else
{
    makecombination(original,empty,comb,k,nc,size);
    k=k+1;
    for(int i=0;i<nc;i++)
    {
        empty[k-1]=comb[i];

        cout<<"k = "<<k<<" nc = "<<nc<<" empty[k-1] = "<<empty[k-1]<<endl;//checks the  value of k , nc, empty[k-1] for proper understanding
        recurse(original,empty,k,size);
    }
}

}

int main()
{
const int size=3;
int k=0;
char original[]="ABC";

char empty[size];
for(int f=0;f<size;f++)
empty[f]='*';

recurse(original,empty,k,size);

cout<<endl<<counter<<endl;
return 0;
}
Sumit Kumar Saha
  • 799
  • 1
  • 12
  • 25