eclipse - Coding Challenge in Java: Given Letters and Returning What Rank They are in -
hey practice found coding challenge have been working on few days. have first part, can't seem figure out how continue am. here challenge:
consider "word" sequence of capital letters a-z (not limited "dictionary words"). word @ least 2 different letters, there other words composed of same letters in different order (for instance, stationarily/antiroyalist, happen both dictionary words; our purposes "aaiilnorstty" "word" composed of same letters these two). can assign number every word, based on falls in alphabetically sorted list of words made of same set of letters. 1 way generate entire list of words , find desired one, slow if word long. write program takes word command line argument , prints standard output number. not use method above of generating entire list. program should able accept word 20 letters or less in length (possibly letters repeated), , should use no more 1 gb of memory , take no more 500 milliseconds run. answer check fit in 64-bit integer. sample words, rank: abab = 2 aaab = 1 baaa = 4 question = 24572 bookkeeper = 10743 nonintuitiveness = 8222334634 program judged on how fast runs , how code written. running program reading source code, can make process easier appreciated.
so far, code have can return correct answer if of letters different. here code:
import java.util.arrays; import java.util.scanner; public class athenadility { public static void main (string[] args) { //finds word 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 (int m = 0; m < word.length(); m++) { (int c = 0; c < word.length()-1; c++) { system.out.println(charm[m] + " " + alphacharm[c]); //skips if alphacharm space if (alphacharm[c] == '-') { } //if same letter breaks , begins next else if (charm[m] == alphacharm[c]) { system.out.println("deleting: " + alphacharm[c]); alphacharm[c] = '-'; //delete letter used , cannot used compare @ later points break; } //if letter in alphacharm comes before charm else if (charm[m] > alphacharm[c]){ system.out.println("found!"); //factorial calculation int factorial = 1; //takes length of word minus current location , 1 after factorial (int f = word.length() - m - 1; f > 0; f--) { system.out.print(f + " "); factorial *= f; } //end loop //adding others system.out.println("\n" + "factorial: " + factorial); value += factorial; } else { } } } //result system.out.println("end: " + value); } }
to try , explain can, creates 2 strings: 1 letters in alphabetical order , 1 original word. program compares each letter @ time , letter comes before 1 in original word alphabetically causes factorial calculation number of combinations exist before first word.
the part need factoring in if strings entered have more 1 of same letter. have literally spent days trying figure out. thank in advance!
p.s. code has lot of system.out.println testing sake
Comments
Post a Comment