Saturday, May 10, 2014

The Alphabetical Ranking of a Word - Java

I recently was faced with this problem.
Given a word. Say we have a list of all the words possible using those letters in the given word in alphabetical order. What is the "rank" of the given word where the rank is the row number of the given word in the list.

Solve this without forming the alphabetical list.

How do we find the ranking of the word?

Let me ask a better question: how do we find the number of words before this word?

If you know about combinatorics, this will be easy for you. If not, then take a look at this on multiset permutations.

The solution I came up with is traverse the letters of the given word and see if there are any letters to the right of the current letter that can come before it.

If there are, for each of those letters pretend that you swap them with the current and calculate the number of unique permutations with the letters to the right of the current position. Finally add the calculated result of unique permutations to a sum.

After finished traversing the word, return the sum.

That's it.

Coding-wise, I used a hashmap of <Char, Integer> to hold the letters of the input string and their counts.

I check if there should be another word that comes before the current word by just comparing it with the rest of the key set.

If there is we comput the number of unique permutations, add to the cummulative sum, then remove the letter from the hashmap if the count is <= 1, otherwise we just decrement its count.

 private long calculateRank(char[] inputArray)
 {
  long rank = 1;
  if(letterCounts == null || letterCounts.isEmpty())
  {
   return 0;
  }

  for(char a : inputArray)
  {
   for(char b : letterCounts.keySet())
   {
    if(b < a)
    {
     rank += getSumOfValuesFactorial()/getProductOfDuplicateFactorial(b);
    }
   }
   if(letterCounts.get(a) <= 1)
   {
    letterCounts.remove(a);
   }
   else
   {
    letterCounts.put(a, letterCounts.get(a)-1);
   }
  }

  return rank;
 }
Source code can be found here.

No comments:

Post a Comment