Let $S$ be a string of length $n$. Each character of $S$ has probability $p$ of being 'A' and probability $1-p=q$ of being 'B'. $R$ is the number of occurrences of the substring 'AB' in $S$. I'd like to determine $\mathbf{E}[R]$ and $\mathbf{var}[R]$ . I can determine the expectation via a recursive definition and total expectation:
$\mathbf{E}[R] = pq\left(1+\mathbf{E}\left[R_{n-1}\right]\right)+\left(1-pq\right)\mathbf{E}\left[R_{n-1}\right]$
But I am not sure how I can approach the problem for determining the variance.
I tried defining a Probability Mass Function but it looks like it will get messy very soon. $\Pr(R=x) = \binom{n-x}{x}\cdot\left(pq\right)^{x} \cdot \text{probability of no other ABs}$
And that "no other" probability ends up being, for example for $x=1$:
$\sum_{i=0}^{u}p^i q^{u-1} \cdot \sum_{i=0}^{v}p^i q^{u-1}$ summed over all $u+v+2=n$ but I think there might be a better way.