permutation - Word ranking efficiency -


i not sure how solve problem within constraints.

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 25 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 

examples:

aaab - 1 aaba - 2 abaa - 3 baaa - 4  aabb - 1 abab - 2 abba - 3 baab - 4 baba - 5 bbaa - 6 

i thought using binary search word , possible words built characters (1 - permutation(word)) think take long. o(logn) might slow.

i found solution bit confused , need bit of understanding it:

consider n-letter word { x1, x2, ... , xn }. solution based on idea word number sum of 2 quantities:  number of combinations starting letters lower in alphabet x1, , how far the arrangements start x1. trick second quantity happens word number of word { x2, ... , xn }. suggests recursive implementation.  getting first quantity little complicated:  let uniqlowers = { u1, u2, ... , um } = unique letters lower x1 each uj, count number of permutations starting uj. add up. 

the solutions says answer consists of 2 numbers. @ following picture describing words can made word question:

eionqstu (first word lexographically, rank 1) ... ... ... (first word before q, rank a) qeionstu  .... .... question (our given word, rank x) ... 

this phrase "how far the arrangements start x1", quantity (x-a), call b. thing b equal word rank of "uestion", our original word first letter cut off. asking same question subset of our input, suggesting recursive solution.

it remains find a, says find number of permutations of words beginning words come before q. = number of words beginning {e, i, o, n}


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -