4

Hey so for practice I found this coding challenge which I have now been working on for a few days. I have the first part, but I just can't seem to figure out how to continue from where I am. Here is the challenge:

    Consider a "word" as any sequence of capital letters A-Z (not limited to 
just "dictionary words"). For any word with at least two different letters, 
there are other words composed of the same letters but in a different order (for 
instance, STATIONARILY/ANTIROYALIST, which happen to both be dictionary words; 
for our purposes "AAIILNORSTTY" is also a "word" composed of the same letters as
these two).

    We can then assign a number to every word, based on where it falls in an 
alphabetically sorted list of all words made up of the same set of letters. One
 way to do this would be to generate the entire list of words and find the 
desired one, but this would be slow if the word is long.
    Write a program which takes a word as a command line argument and prints to 
standard output its number. Do not use the method above of generating the entire
 list. Your program should be able to accept any word 20 letters or less in 
length (possibly with some letters repeated), and should use no more than 1 GB
 of memory and take no more than 500 milliseconds to run. Any answer we check 
will fit in a 64-bit integer.

Sample words, with their rank:
ABAB = 2 
AAAB = 1 
BAAA = 4 
QUESTION = 24572 
BOOKKEEPER = 10743 
NONINTUITIVENESS = 8222334634

    Your program will be judged on how fast it runs and how clearly the code is 
written. We will be running your program as well as reading the source code, so 
anything you can do to make this process easier would be appreciated.

So far, the code I have can return the correct answer if all of the letters are different. Here is my code:

import java.util.Arrays;
import java.util.Scanner;
public class AthenaDility {

    public static void main (String[] args) {
        //Finds word that is entered
        Scanner scan = new Scanner (System.in);
        String word = scan.next();
        scan.close();

        //added value
        int value = 1;

        //alphabetical representation
        char[] charm = word.toCharArray();
        char[] alphaCharm = word.toCharArray(); 
        Arrays.sort(alphaCharm);

        //Comparer
        for (int m = 0; m < word.length(); m++) {
            for (int c = 0; c < word.length()-1; c++) {
                System.out.println(charm[m] + " " + alphaCharm[c]);

                //Skips if alphaCharm is a space
                if (alphaCharm[c] == '-') {
                }

                //If the same letter it breaks look and begins next
                else if (charm[m] == alphaCharm[c]) {
                    System.out.println("Deleting: " + alphaCharm[c]);
                    alphaCharm[c] = '-'; //Delete letter for it is used and cannot be used to compare at later points
                    break;
                }

                //if the letter in alphaCharm comes before charm
                else if (charm[m] > alphaCharm[c]){
                    System.out.println("Found!");

                    //factorial calculation
                    int factorial = 1;

                    //takes the length of the word minus the current location and one after for factorial
                    for (int f = word.length() - m - 1; f > 0; f--) {
                        System.out.print(f + " ");
                        factorial *= f;
                    }
                    //end loop
                    //Adding to others
                    System.out.println("\n" + "Factorial: " + factorial);
                    value += factorial;
                }
                else {

                }
            }
        }

        //Result
        System.out.println("end: " + value);
    }

}   

To try and explain it as simply as I can, it creates two strings: one is the letters in alphabetical order and one is the original word. The program then compares each letter at a time and any letter that comes before the one in the original word alphabetically causes a factorial calculation for the number of combinations that would exist before the first word.

The part I need help factoring in is if the strings entered have more than one of the same letter. I have literally spent DAYS trying to figure this out. Thank you in advance!

p.s. the code has a lot of System.out.println for testing sake

Kiley
  • 409
  • 1
  • 5
  • 19
  • Why this tagged with `Python`? You want somebody to re-write this code in Python?! – Andersson Jun 25 '15 at 14:05
  • @Andersson the tag has already been removed – Olivier Poulin Jun 25 '15 at 14:06
  • Because a lot of answers I have found online were in Python and I thought someone could at least interpret them better than the person who posted them so I could use the "strategy" for lack of a better word. – Kiley Jun 25 '15 at 14:09
  • If you don't get a great response here, you may want to try the Code Review Stack Exchange. – Alex Jun 25 '15 at 16:32
  • Thanks @Alex I will definitely take a look if I do not hear anything here! – Kiley Jun 25 '15 at 17:53
  • 1
    See this shortest java code : http://stackoverflow.com/questions/22642151/finding-the-ranking-of-a-word-permutations-with-duplicate-letters – Rajesh Jun 25 '15 at 18:36
  • @Rajesh I took a look but it seems like that code does not work correctly in java. – Kiley Jun 25 '15 at 18:51
  • 1
    see Java version:`public static long rankPerm(String perm) {}` in accepted answer. I have tested all words you have shared and got accurate answer. – Rajesh Jun 25 '15 at 18:53
  • Thank you very much @Rajesh! I tested it and it works! – Kiley Jun 25 '15 at 19:09

0 Answers0