TITLE: How long until a random word with letters "A", "B", "C" ends in the pattern "ABC"?
QUESTION [9 upvotes]: Let's say I have word constructed from random letters, A Band C with $\mathbf{P}(A) = \mathbf{P}(B) = \mathbf{P}(C) = \frac{1}{3}$.  I am going to do a random trial and record the letters I got.  The experiment stops the first time I spell out the word ABC.  Let $N$ be the number of trials until I make the word ABC out of letters.
Here are some trial words:
BBBBACCCCBABAABBBBCBCCBBBCACBCAACBABC
BBACCCCACABABC
CBBCCCABBABC
BABBBCAAAABC
CBBBCCBCCABABC
CCBCBBABC
ACCACCCCBCBBBCBACCBBAABBABBACCCBCBAABC
ABAAABBBABC
ABABC
BBCACAACCACCAABAAABBCABBBBACABACBACBAABACCCBCBCCCBCCCBAAAABC

I am asking for the expected length of this word.  And the variance.  

$\mathbb{E}[N]$ expectation
$\mathbb{E}[N^2] - \mathbb{E}[N]^2$ variance


Sounding more like a textbook:
Our random variable is $X \in \{ A,B,C\}$ where each letter appears with equal probability.  Let's examine the sequence $(X_1, X_2, X_3, \dots , X_n)$ where $X_i$ are iid random variables with probability the same as $X$.  Our process stops at time $t = N$ when $(X_{N-2}, X_{N-1}, X_N) = (A,B,C)$.  What is the expected value of $N$ ?

REPLY [2 votes]: Instead of using the apparatus of Markov chains, the result for the mean can be immediately obtained using Kac's theorem about return times (an important result that is often used, eg, for proving asympotic optimality of Lempel-Ziv compression algorithms - see eg Cover & Thomas, 13.5).
In this case, the probability of success of the ergodic $0$-$1$ process is $p=(1/3)^3=1/27$, hence the mean return time is $\langle T \rangle = 1/p=27$
The computation of the variance seems to be much more difficult. Some work in "Variations on a Theme by Mark Kac" (P. W. Kasteleyn , Journal of Statistical Physics, Vol. 46, Nos. 5/6, 1987).