Consider sequences of numbers 0, 1, 2 with length n. There are $3^n$ such sequences.
I want to know how many sequences there are that contain a k-run of 1's followed by 2. As a regular expression:
(^|.*[^1])[1]{k}[2].*
Even better would be to know the number of sequences that contain a maximal k-run of 1's followed by 2 i.e. that contain no other K-run with K > k.
Let $\#(k)$ be the number of sequences that contain a maximal k-run of 1's followed by 2.
These are obvious conditions, #(k) must fulfil:
$\#(n-1) = 1$
$\sum_{k = 0..n-1} \#(k)= 3^n$
I am looking for a closed form for $\#(k)$. If this is too hard to achieve, I would be happy with a closed form for the number of sequences that contain an arbitrary (not necessarily maximal) k-run of 1's followed by 2.
This is a just a partial answer, but there was not enough space in a comment.
The problem seems not so easy. It is easy to see that the number of sequences of length $n$ that do contain "$12$" are $$ 3^n - F_{2n+2}\, $$ where $F_k$ denotes the $k$-th Fibonacci number.
The resulting sequence $0, 1, 6, 26, 99, 352, 1200, 3977,\dots$ is in the OEIS as Sequence A186314, i.e. Number of ternary strings of length n which contain 01. You can follow the link for more details.
Looking for $112$, things escalate quickly. Indeed, the resulting sequence is not in the OEIS, but the complement sequence ($3^n-a(n)$) is there, as Sequence A076264 aka Number of ternary (0,1,2) sequences without a consecutive '012'.
This sequence can be described easily with a recurrence, but can also be described with a sum. Putting together the OEIS info we got that the number of sequences of length $n$ that contain "$112$" are $$ 3^n-\sum_{k=0}^{\lfloor n/3\rfloor}(-1)^k {n-2k\choose k} 3^{n-3k} $$
Maybe you or somebody else can generalize upon these partial results. I don't know if looking for maximal subsequences makes the problem easier or harder.