Sub topic 14
Rank of a Word in Dictionary Order
Assume you know factorials, permutations, and combinations. This chapter assumes that all letters in the word are distinct. Repeated letters require dividing by factorials of repeated counts, which is a separate adjustment.
The dictionary rank of a word is its position among all arrangements of its letters, listed in alphabetical order. Rank counting starts from 1.
The Ranking Method
To find the rank, scan the word from left to right:
- At the current position, count the unused letters that are alphabetically smaller than the current letter.
- Each smaller letter would create a block of arrangements.
- Count the arrangements in each block using the factorial of the remaining positions.
- Add these counts to the rank.
- Mark the current letter as used and continue.
For a word with distinct letters, the number of arrangements after fixing one letter is:
\[ (\text{number of unused letters})! \]
Worked Example: CAT
The letters are A, C, and T. Their alphabetical order is A, C, T.
- Start with rank \(1\).
- At the first position, C has one smaller unused letter: A. If A came first, the remaining letters could be arranged in \(2!\) ways. Add \(1 \times 2! = 2\).
- Use C. At the second position, A has no smaller unused letter. Add \(0\).
- Use A. At the third position, T has no smaller unused letter.
Therefore:
\[ \text{rank} = 1 + 2 = 3 \]
The arrangements confirm this:
- ACT
- ATC
- CAT
Formula and Python Check
For a word \(w\) with distinct letters:
\[ \text{rank} = 1 + \sum (\text{smaller unused letters}) \times (\text{remaining positions})! \]
import math
def word_rank(word):
rank = 1
unused = list(word)
for i, letter in enumerate(word):
smaller = sum(ch < letter for ch in unused)
remaining = len(unused) - 1
rank += smaller * math.factorial(remaining)
unused.remove(letter)
return rank
print(word_rank("CAT")) # 3The comparison ch < letter uses alphabetical order for uppercase English letters.
Common Errors and CAT Strategy
- Count only letters that are still unused.
- Multiply by the factorial of the positions after the current position.
- Do not count the current letter as smaller than itself.
- Do not sort the word and then use the original positions without updating the unused letters.
- For a quick check, the rank must lie between 1 and \(n!\), where \(n\) is the number of distinct letters.
Exercise
Find the dictionary rank of the word BAD among all arrangements of A, B, and D.
Show answer
2. The arrangements begin ABD, ADB, BAD, so BAD is third? Recheck: At B, A is smaller, giving 2! arrangements. Thus rank = 1 + 2 = 3.