A coin lands on heads at a probability $p\in\left(0,\frac12\right)\cup\left(\frac12,1\right)$ when tossed. We continue to toss the coin until we get head-head-head or head-tail-head in a row. Find the probability of ending at head-head-head.
I assumed that the probability that the last two tosses
- are head-head but we don't stop is $A_n$.
- are tail-head but we don't stop is $B_n$.
- are tail-tail but we don't stop is $C_n$.
- are head-tail but we don't stop is $D_n$.
We have $n\ge3$, \begin{cases}A_n=pB_{n-1},&(1)\\\\B_n=pC_{n-1},&(2)\\\\C_n=(1-p)C_{n-1}+(1-p)D_{n-1},&(3)\\\\D_n=(1-p)A_{n-1}+(1-p)B_{n-1}.&(4)\end{cases}
I now have a solution, but the method differs from lulu's:
Plug $(1)\to(4)$, $(2)\to(3)$, \begin{cases} D_n=(1-p)B_{n-1}+p(1-p)B_{n-2},\\[1em]\frac1pB_{n+1}=(1-p)D_{n-1}+\frac{1-p}pB_n. \end{cases} Hence, for $n\ge5$, \[B_{n+1}=(1-p)B_n+p(1-p)^2B_{n-2}+p^2(1-p)^2B_{n-3}.\] Now consider \[\sum_{n\ge5}B_{n+1}\!=\!(1-p)\sum_{n\ge5}B_n\!+\!p(1-p)^2\sum_{n\ge5}B_{n-2}\!+\!p^2(1-p)^2\sum_{n\ge5}B_{n-3}.\] Let $s=\sum\limits_{n\ge2}B_n$. Now \begin{align}s\!-\!B_2\!-\!B_3\!-\!B_4\!-\!B_5=\begin{aligned}s(1-p)\!-\!(B_2+B_3+B_4)(1-p)\\\\+sp(1-p)^2\!-\!B_2~p(1-p)^2\!+\!sp^2(1-p)^2.\end{aligned}\tag5\end{align} Since \[B_2=p-p^2,~B_3=p+p^3-2p^2,~B_4=p-2p^2+p^3,\] Solving $(5)$ gives \[\sum_{n\ge2}B_n=s=\frac{1+p^4-p^3-p^3}{p+p^2-p^3}.\] So \[\mathrm P(\text{HHH})=p\sum_{n\ge2}A_n=pA_2+p^2\sum_{n\ge2}B_n=\boxed{\frac{p}{p+1-p^2}}.\]