# Expected number of flips for $THT$ sequence

A Coin with Probability of heads $p$ is continously flipped till the sequence $THT$ occurs.Find the Expected number of coin flips.

My Try: if $X$ is number of coin flips till $THT$ occurs, $X$ can take values of $3$, $4$,$5$,$\cdots$

$$P(X=3)=(1-p)^2p$$

For $X=4$ the Sample space is $HTHT$ or $TTHT$

$$P(X=4)=(1-p)^2p$$

For $X=5$ the Sample space is $HHTHT$ or $HTTHT$ or $TTTHT$

$$P(X=5)=(p^2-p+1)(1-p)^2p$$ But i feel the process becomes tedious as $X$ increases. Please suggest me any hint...

-
See this answer also: math.stackexchange.com/questions/596785/… –  gar May 3 '14 at 11:30
Got something from the answer below? –  Did May 8 '14 at 9:10

Consider the set of prefixes of the word $tht$, that is, the set $\{o,t,th,tht\}$ where $o$ denotes the empty prefix. For every prefix $w$ let $m_w$ denote the mean number of draws needed to produce the word $tht$ starting from a sequence of draws which ends by $w$ but not by any longer prefix.
Thus, $m_{tht}=0$ and the question is to compute $m_o$. Let $q=1-p$. The usual one-step decomposition yields the identities $$m_o=1+pm_o+qm_t,\quad m_t=1+pm_{th}+qm_t,\quad m_{th}=1+pm_o,$$ and solving this affine system yields $$m_o=\frac1q+\frac1{pq^2}.$$ An approach to compute the mean time necessary to produce any word $w$ in terms of the structure of its prefixes (and an explanation of the formula above) is in the book DNA, Words And Models by Robin, Rodolphe, and Schbath. The authors explain why this mean time is the sum of the inverses of the probabilities of each prefix of $w$ which is also a suffix of $w$. When $w=tht$, these prefixes-suffixes are $tht$ and $t$, whose probabilities are $pq^2$ and $q$ respectively, hence the formula above.