CLR cover

Problem 111: Letter-Occurrence Differences

For a non-empty word $x$, $\dif(x)$ denotes the difference between the numbers of occurrences of the most frequent letter and of the least frequent letter in $x$. (they can be the same letter.)

Design an $O(n|A|)$ running time algorithm computing the value $\max\{\dif(x) \mid x \mbox{ factor of } y\}$ for a non-empty word $y$ of length $n$ over the alphabet $A$.
First consider $A=\{\sa{a},\sa{b}\}$.