title: corresponding permutation number
body: <p>I have a given word, for wich I need to find its number of permutation on its corresponding sorted word . 
  Say I have word BABA , its corresponding sorted word would be, AABB, if I start permuting this sorted word, would come to AABB as a second "word", regardless of letter repetition, then ABAB, ABBA , BABA .. so the permute number for word BABA is 5 .
  The easy way would be start doing all possible combinations, and then compared with the initial word .
  so far , ive done..</p>
  
  <pre><code>import java.util.Arrays;
  
  public class Permutation {
   int location =1;
  public static char[]  warray;
  
  void printArray(char []a) {
   for (int i = 0; i &lt; a.length; i++) {
   System.out.print(a[i]+" ");
   }
    System.out.println("location " + location );
  }
   void permute(char []a,int k ) {
    if(k==a.length) {
       location++;
    // Check if the permuted word is the one looking for.
   if (Arrays.equals(a, warray))
    { System.out.println("final iteration k" + k);        
    printArray(a); 
    System.exit(0);}
    }
  else
     for (int i = k; i &lt; a.length; i++) {
     char temp=a[k];
     a[k]=a[i];
     a[i]=temp;
      permute(a,k+1);
     }
  }
  
  public static void main(String[] args) {
    if (args[0].length() &gt; 25 )  {
       System.out.println(" Word not in permited range " );        
       System.exit(0);
   }
   else {
   Permutation p=new Permutation();
   warray = new char[args[0].length()];
    char [] wpermute = new char[args[0].length()];
  
  for (int i = 0; i &lt; args[0].length(); i++) {
       warray[i] = new Character(args[0].charAt(i));
       wpermute[i] = new Character(args[0].charAt(i));
   }
  
  Arrays.sort(wpermute);
  
  System.out.print("sorted word : " );
  for (int i = 0; i &lt; wpermute.length; i++) {
    System.out.print(wpermute[i]);
    }
    p.permute(wpermute,0);
    }
  }
  </code></pre>
  
  <hr>
  
  <p>But this could be very slow performance.
  My second guess, would be , starting like a binary search startting with first letter of unsorted word, calculate possibble permutations to have this letter as the first letter on permutations, and then second letter..and so ... would that sound good ?</p>