title: Permutation of String letters: How to remove repeated permutations?
body: <p>Here is a standard function to print the permutations of characters of a string:</p>
  
  <pre><code>void permute(char *a, int i, int n)
  {
     int j;
     if (i == n)
       printf("%s\n", a);
     else
     {
          for (j = i; j &lt; n; j++) //check till end of string
         {
            swap((a+i), (a+j));
            permute(a, i+1, n);
            swap((a+i), (a+j)); //backtrack
         }
     }
  } 
  
  void swap (char *x, char *y)
  {
      char temp;
      temp = *x;
      *x = *y;
      *y = temp;
  }
  </code></pre>
  
  <p>It works fine but there is a problem, it also prints some duplicate permutations, exapmle:</p>
  
  <p>if string is "AAB"</p>
  
  <p>the output is:</p>
  
  <pre><code>AAB
  ABA
  AAB
  ABA
  BAA
  BAA
  </code></pre>
  
  <p>This is having 3 duplicate entries as well.</p>
  
  <p>Can there be a way to prevent this to happen?</p>
  
  <p>--</p>
  
  <p>Thanks</p>
  
  <p>Alok Kr.</p>