Sub topic 14
Rank of a Word in Dictionary Order
What this chapter assumes
You should know factorials, the count of arrangements of n distinct objects (n!), and the count for repeated letters (n! divided by the factorial of each repeat count). If those feel shaky, revisit the chapters on factorials and on repeated objects first.
The question
Take all the letters of a word and write every rearrangement in alphabetical order, whether or not the result is a real word. The rank of a word is its position in that list, starting from 1.
CAT rearranges into six words: ACT, ATC, CAT, CTA, TAC, TCA. CAT is third, so its rank is 3.
CAT questions usually come in two forms:
- Given a word, find its rank.
- Given a rank, find the word.
Writing out the list works for three letters and fails for six. The method below works at any length.
The core idea
Count how many words come before the target. Then add 1.
A word comes before the target if, at the first position where they differ, it has a smaller letter. So scan the target left to right. At each position, ask:
- Which unused letters are smaller than the target's letter here?
- For each such letter, how many words start with the same prefix and then that smaller letter? The remaining positions can be filled in any order, so the count is (remaining positions)!.
Add these counts across all positions, then add 1 for the target itself.
Worked example: all letters distinct
Find the rank of RANK. Its letters in alphabetical order are A, K, N, R, and there are 4! = 24 words in total.
| Position | Target letter | Unused smaller letters | Count | Words per choice | Added |
|---|---|---|---|---|---|
| 1 | R | A, K, N | 3 | 3! = 6 | 18 |
| 2 | A | none | 0 | 2! = 2 | 0 |
| 3 | N | K | 1 | 1! = 1 | 1 |
| 4 | K | none | 0 | 0! = 1 | 0 |
The words before RANK number 18 + 0 + 1 + 0 = 19, so the rank is 20.
Check: the words starting with R are the last six, ranks 19 to 24. The first two are RAKN (19) and RANK (20). This matches.
The first code example from the plan does this calculation:
import math
def word_rank(word):
# Rank of a word with all distinct letters, counting from 1
n = len(word)
rank = 1
for i, ch in enumerate(word):
smaller_after = sum(1 for c in word[i + 1:] if c < ch)
rank += smaller_after * math.factorial(n - i - 1)
return rank
print(word_rank("CAT")) # 3 (ACT, ATC, CAT)
print(word_rank("RANK")) # 20The code counts smaller letters only among the letters after position i. When all letters are distinct, those are exactly the unused letters, so this is the same as step 1 above.
Repeated letters
When letters repeat, two things change:
- Count each distinct smaller letter once, not once per copy.
- The count for each choice is no longer (remaining)!. It is the number of arrangements of the remaining multiset: remaining! divided by the factorial of each repeat count.
Find the rank of BANANA. The letters are A, A, A, B, N, N, in alphabetical order. There are 6!/(3! × 2!) = 60 words in total.
- Position 1 is B. The only smaller letter is A. Fix A first and arrange A, A, B, N, N: 5!/(2! × 2!) = 30.
- Position 2 is A. After B is used, the unused letters are A, A, A, N, N. No letter is smaller than A, so add 0.
- Position 3 is N. The unused letters are A, A, N, N. The smaller letter is A. Fix A and arrange A, N, N: 3!/2! = 3.
- Position 4 is A. The unused letters are A, A, N. Nothing is smaller, so add 0.
- Position 5 is N. The unused letters are A, N. The smaller letter is A. Fix A and arrange N: 1.
- Position 6 is A. Add 0.
The words before BANANA number 30 + 3 + 1 = 34, so the rank is 35.
This code handles repeats by trying each smaller distinct letter and counting the arrangements of what remains:
import math
from collections import Counter
def multiset_perms(counts):
# Arrangements of a multiset given as {letter: count}
total = math.factorial(sum(counts.values()))
for c in counts.values():
total //= math.factorial(c)
return total
def word_rank_repeats(word):
counts = Counter(word)
rank = 1
for ch in word:
for smaller in sorted(counts):
if smaller >= ch:
break
if counts[smaller] > 0:
counts[smaller] -= 1
rank += multiset_perms(counts)
counts[smaller] += 1
counts[ch] -= 1
return rank
print(word_rank_repeats("CAT")) # 3
print(word_rank_repeats("BANANA")) # 35Finding the word at a given rank
Reverse the process. Work with rank − 1, which is the number of words that must come before the answer. At each position, divide by the block size (the factorial of the remaining positions). The quotient says how many letters to skip in the sorted list of unused letters. The remainder carries to the next position.
Find the 20th word from the letters of RANK. The sorted letters are A, K, N, R, and rank − 1 = 19.
| Position | Sorted unused letters | Block size | Quotient, remainder | Letter chosen |
|---|---|---|---|---|
| 1 | A, K, N, R | 6 | 19 ÷ 6 = 3 r 1 | R (skip 3) |
| 2 | A, K, N | 2 | 1 ÷ 2 = 0 r 1 | A |
| 3 | K, N | 1 | 1 ÷ 1 = 1 r 0 | N |
| 4 | K | 1 | 0 | K |
The word is RANK, which agrees with the forward calculation.
With repeated letters, the block size is the multiset count after removing the chosen letter. The blocks are no longer equal in size, so subtract block by block instead of dividing.
Common errors
- Counting letters smaller than the target in the whole word. Count only the letters not yet used. Letters already fixed in earlier positions are not available.
- Counting a repeated smaller letter twice. Two copies of A give one block of words that begin with A at that position, not two.
- Using (remaining)! when letters repeat. Divide by the factorials of the repeat counts of the remaining letters.
- Forgetting that the list includes meaningless words. Dictionary order here covers every arrangement, not only real words. If a question says otherwise, it will say so.
- Mixing up positions from the left and from the right. Rank from the end is total − rank + 1. Use this when the target is close to the last word, such as one starting with the largest letter.
Quick checks under time pressure
- The first word is the letters in alphabetical order, with rank 1.
- The last word is the letters in reverse order, with rank equal to the total count.
- If the target starts with the smallest letter, position 1 adds nothing, and you can ignore that letter and rank the rest.
- A target that starts with the largest letter lies in the last block. Its rank is at least total − (n − 1)! + 1.
Exercise
Find the rank of LEAD among all arrangements of its letters.
Show answer
Sorted letters are A, D, E, L, with 24 words in total. Position 1 is L: three smaller letters (A, D, E) times 3! = 18. Position 2 is E: unused letters are A, D, E, so A and D are smaller, and 2 × 2! = 4. Position 3 is A: nothing smaller, so 0. Position 4 is D: 0. Words before LEAD: 18 + 4 = 22, so the rank is 23.
Exercise
Find the rank of MOON among all distinct arrangements of its letters.
Show answer
Sorted letters are M, N, O, O, with 4!/2! = 12 words. Position 1 is M, the smallest letter, so add 0. Position 2 is O: the unused letters are N, O, O, and the only smaller letter is N. Fixing N leaves O, O, which arrange in 1 way, so add 1. Position 3 is O: the unused letters are N, O, and the smaller letter N leaves O, so add 1. Position 4 is N: add 0. Words before MOON: 2, so the rank is 3. The first three words are MNOO, MONO, MOON.
Exercise
What is the 15th word in the dictionary list of the letters A, E, L, R (all four letters used once)?
Show answer
Use rank − 1 = 14. Position 1: block size 3! = 6, and 14 ÷ 6 = 2 remainder 2, so skip two letters of A, E, L, R and choose L. Position 2: unused A, E, R, block size 2, and 2 ÷ 2 = 1 remainder 0, so choose E. Position 3: unused A, R, block size 1, and 0 ÷ 1 = 0, so choose A. Position 4: R. The word is LEAR.