Monkey problem
To settle down which monkey is faster on average, I'll use Markov chains and Mathematica. Define a state $i$, for $i = 0..6$, as that the monkey 1 has currently written $i$ correct subsequent characters of COCONUT, but has never written all of COCONUT. Define a state 7 as the monkey 1 having written COCONUT at least once. Since state 7 cannot be escaped, we say that it is absorptive (other states are transient). The transition matrix for monkey 1 is:
P1 = {
{25, 1, 0, 0, 0, 0, 0, 0},
{24, 1, 1, 0, 0, 0, 0, 0},
{25, 0, 0, 1, 0, 0, 0, 0},
{24, 1, 0, 0, 1, 0, 0, 0},
{24, 0, 0, 1, 0, 1, 0, 0},
{24, 1, 0, 0, 0, 0, 1, 0},
{24, 1, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 26}
} / 26;
Here $P1_{ij}$ is the probability that the monkey 1 transitions from state $i$ to state $j$ on pushing a key. The Markov chain can be visualized as a graph:
p1 = DiscreteMarkovProcess[{1, 0, 0, 0, 0, 0, 0, 0}, P1];
Graph[Table[StringTake["COCONUT", x], {x, 0, 7}], p1,
VertexSize -> 0.6, GraphLayout -> {VertexLayout -> "CircularEmbedding" }]

Next up, we extract the sub-matrix which does not contain the absorptive state:
Q1 = P1[[1 ;; 7, 1 ;; 7]];
As described here, the expected time for the monkey 1 to write COCONUT (expected time to absorption) starting from state $i$ is given by
N1 = Inverse[IdentityMatrix[7] - Q1]
t1 = N1.{1, 1, 1, 1, 1, 1, 1};
The exact solution is given by:
- 8031810176
C 8031810150
CO 8031809500
COC 8031792574
COCO 8031352524
COCON 8019928800
COCONU 7722894400
The transition matrix for monkey 2 is given by:
P2 = {
{25, 1, 0, 0, 0, 0, 0, 0},
{24, 1, 1, 0, 0, 0, 0, 0},
{24, 1, 0, 1, 0, 0, 0, 0},
{24, 1, 0, 0, 1, 0, 0, 0},
{24, 1, 0, 0, 0, 1, 0, 0},
{24, 1, 0, 0, 0, 0, 1, 0},
{24, 1, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 26}
} / 26;
The Markov chain for monkey 2 visualized as a graph:
p2 = DiscreteMarkovProcess[{1, 0, 0, 0, 0, 0, 0, 0}, P2];
Graph[Table[StringTake["TUNOCOC", x], {x, 0, 7}], p2,
VertexSize -> 0.6, GraphLayout -> {VertexLayout -> "CircularEmbedding" }]

By following the same procedure as for monkey 1, we solve $t2$ as:
- 8031810176
T 8031810150
TU 8031809500
TUN 8031792600
TUNO 8031353200
TUNOC 8019928800
TUNOCO 7722894400
Because the monkeys start from the situation when nothing has been written yet, we see that the expected time for them to write their word is the same $C = 8031810176$
This result can also be obtained directly in Mathematica:
d1 = FirstPassageTimeDistribution[p1, 8];
Mean[d1]
In fact, we can compute the characteristic function for the absorption-time-distribution:
CharacteristicFunction[d1, x]
which evaluates to
e^(7ix) / (C - C e^(ix) + e^(7ix))
The distributions for both monkeys have this same characteristic function. Wikipedia claims that cumulative distribution functions and characteristic functions are in one-to-one correspondence. This implies that the monkeys' finishing-time-distributions are the same.
Squirrel problem
The transition matrix for a monkey aiming for both words COCONUT and TUNOCOC is:
P3 = {
{24, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{23, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{24, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{23, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{23, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{23, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{24, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{23, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{23, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
{23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0},
{24, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{23, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0},
{24, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26}} / 26
Here the states have been numbered in order - C CO COC COCO COCON COCONU T TU TUN TUNO TUNOC TUNOCO COCONUT TUNOCOC. Note that the absorbing states come last. The same reference as above shows that we can compute the probability $B3_{ij}$ of ending from a transient state $i$ to an absorption state $j + 13$ as follows:
Q3 = P3[[1 ;; 13, 1 ;; 13]];
R3 = P3[[1 ;; 13, 14 ;; 15]];
N3 = Inverse[IdentityMatrix[13] - Q3]
B3 = N3.R3
The matrix $B3 * 617830874$ is
COCONUT TUNOCOC
- 308915099 308915775
C 308915100 308915774
CO 308915125 308915749
COC 308915776 308915098
COCO 308932701 308898173
COCON 309372075 308458799
COCONU 320796475 297034399
T 308915098 308915776
TU 308915073 308915801
TUN 308914423 308916451
TUNO 308897523 308933351
TUNOC 308458124 309372750
TUNOCO 297033749 320797125
Since the monkey starts from nothing, the probability for writing COCONUT and TUNOCOC is 308915099 and 308915775, respectively, divided by 617830874. These correspond to 0.4999994529 and 0.5000005471, computed with Mathematica accurate to 10 decimal places. Therefore the monkey is more probable to write TUNOCOC than COCONUT.
Code
Here is the Python-code I used to generate the transition matrices.
def computeTransitionMatrix(words, keys):
def properPrefixes(word):
return (word[:i] for i in range(len(word)))
def suffixesInDecreasingLength(word):
return (word[i:] for i in range(len(word) + 1))
prefixToState = {}
stateToPrefix = []
def addState(prefix):
if prefix in prefixToState:
return
prefixToState[prefix] = len(stateToPrefix)
stateToPrefix.append(prefix)
# Create a state for each proper prefix of the word.
for word in words:
for prefix in properPrefixes(word):
addState(prefix)
# Create a state for each word last.
for word in words:
addState(word)
print(stateToPrefix)
# Number of states.
nStates = len(stateToPrefix)
# Compute the (scaled) transition probabilities.
transitions = []
for i in range(nStates):
row = [0] * nStates
transitions.append(row)
prefix = stateToPrefix[i]
if prefix in words:
# The word is an absorptive state.
row[i] = len(keys)
continue
for key in keys:
nextPrefix = prefix + key
# Find the longest suffix which
# is a prefix of the word.
for suffix in suffixesInDecreasingLength(nextPrefix):
j = prefixToState.get(suffix)
if j != None:
row[j] += 1
break
return transitions
# The words the monkey is supposed to write.
words = ['COCONUT', 'TUNOCOC']
# Keys available on monkey's keyboard.
keys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
transitions = computeTransitionMatrix(words, keys)
print(repr(transitions).replace('[', '{').replace(']','}').replace('}, ', '},\n'))