1

There are some standard dictionary problems like given a word bottle, one can find the rank of this word alphabetic order by making sperate cases, but my question is that if you can choose any three letters from this word bottle and arranged it, then how could one can find the rank of this word for example find the rank of the word btt or bol assuming any letter in b,o,t,l,e can come in this new word and t can come two times out of three letters in this new word or permutation formed.

So in general if you have a word with n letters which can repeat and you form a another word or a permutation with k letters out of given n word letter how to find the rank of that word.

1 Answers1

2

As far as I know, there is no nice answer if the word can have repeated letters.

For any multiset $M$ of letters, let $P(M,k)$ be the set of $k$ letter words you can make using letters of $M$. For example,

$$P\big(\{A,A,B,C\} , 2\big)=\{AA,AB,AC,BA,BC,CA,CB\}$$

If $M$ consists of $m$ distinct letters, then $|P(M,k)|=m!/(m-k)!$.

Assuming you can compute $|P(M,k)|$, then you can compute the rank of a word $w$ in $P(M,k)$ has follows. Let $\ell$ be the first letter of $w$, and let $w'$ be the word obtained by removing $\ell$ from the beginning of $w$. Then $$ \text{rank of $w$ in $P(M,k)$} = \left(\sum_{a\in M,a<\ell}|P(M-\{a\},k-1)|\right) +\text{rank of $w'$ in $P(M-{\ell},k-1)$} $$ The first summation ranges over all letters $a$ in $M$ which are alphabetically before $\ell$, and $M-\{a\}$ means you remove one copy of $a$ from $M$. This is a recursive formula, so you need to apply the same formula to compute the rank of $w'$, and then apply this to $w''$, etc.

Edit: To explain why this is true; for each $a<\ell$, $|P(M-\{a\},k-1)|$ counts all of the words in $P(M,k)$ which start with $a$. These all come before $w$, so they increase its rank. The recursive part is comparing $w$ with the words in $P(M,k)$ that start with the same first letter.

Unfortunately, there is no simple "formula" to compute $|P(M,k)|$. This problem gets asked a lot: see Permutation Problem: Select m objects from n ones of r types by sampling without replacement and the linked questions in my comment for some discussion.

Example: Let $M=\{A,A,A,B,C,C\}$, and say you want to find the rank of $CBA$ among all three-letter words made of the letters of $M$. Then \begin{align} \text{rank of $CBA$ in $P(AAABCC,3)$} &= \overbrace{|P(AABCC,3)|}^{\text{remove A}} +\overbrace{|P(AAACC,3)|}^{\text{remove B}} \\&\quad+ \text{rank of $CBA$ in $P(AAABC,2)$} \\\\ \text{rank of $BA$ in $P(AAABC,2)$} &= |P(AABC,3)| \\&\quad+ \text{rank of $A$ in $P(AAAC,1)$} \\\\ \text{rank of $A$ in $P(AAABC,1)$} &= 0^{\quad\text{no letters before $a$, so this is empty sum}} \\&\quad+ \text{rank of $\varnothing$ in $P(AAC,0)$} \\\\ \text{rank of $\varnothing$ in $P(AAC,0)$} &= 1^{\quad\text{since length is $0$, only word is empty word, which has rank $1$}} \end{align} Finally, you get the rank of $CAB$ is $|P(AABCC,3)|+|P(AAACC,3)|+|P(AABC)|+1$. Again, to compute these sizes, look at the links I mentioned.

Mike Earnest
  • 84,902