title: java - string permutation without duplication elements
body: <p>I am using this code <a href="https://stackoverflow.com/a/4240323/2655623">https://stackoverflow.com/a/4240323/2655623</a> to create permutation and do some calculation on each result.</p>
  
  <pre><code>public static void permutation(String str) { 
      permutation("", str); 
  }
  
  private static void permutation(String prefix, String str) {
      int n = str.length();
  
      int p = prefix.length();
      if(p==5){
              //do some calculation for |prefix| = 5
              return;
      }
      for (int i = 0; i &lt; n; i++){
              permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
      }
  }
  </code></pre>
  
  <p>this algorithm works great for me. However, I would like to see how I can remove duplicate characters so I don't calculate the prefix with again. for example for </p>
  
  <pre><code>permutation("aacccbbdeeeef");
  </code></pre>
  
  <p>I will process <strong>abcde</strong> about </p>
  
  <pre><code>A = 2 * 4*3*2*1 when a----
  B = 2 * 4*3*2*1 when b----
  C = 3 * 4*3*2*1 when c----
  and so on...
  // I hope you get the idea
  </code></pre>
  
  <p>I wonder if i can reduce the amount of duplicate calculation.</p>
  
  <p>One way I thought was to sort the character orders, and only calculate one of each duplicate characters when I use FOR for them.</p>
  
  <pre><code>for (int i = 0; i &lt; n; i++){
      if(i&gt;0 &amp;&amp; str.charAt(i) == str.charAt(i-1)) continue;
      permutation.....
  }
  </code></pre>
  
  <p>this worked fine for me as just need the permutation once. It drastically reduce the number of calls when the number of repeated letters are high.</p>
  
  <p>Now, to sum up, I would like to know if </p>
  
  <ol>
  <li>is this guarantee than I will not miss any permutation?</li>
  <li>how can I prevent cases like a(1)ba(2)cd and a(2)ba(1)cd from
  happening when p=5. As for p=8 or 10, the trick I used will not be that efficient. so what I need to do?</li>
  </ol>
  
  <p>Thank you very much.</p>