Find the maximum number of contiguous occurrences of smaller string s in a larger string l Say we have two strings s and l, and they only have characters 'a' through 'z' (lowercase English alphabet characters). The task is to find the maximum number of consecutive occurrences of s in l.
Ex.
s = &quot;ab&quot;
l = &quot;defgababajklabe&quot;
The answer here would be 2, because there is &quot;abab&quot; which would be 2 consecutive occurrences of string s in l.
My initial thought was to go through each character in l, and try to match the substring starting at that position with s, if it matched we would increment our count and keep track of some global maximum. We would also set the value of our next starting point to one position after the end of this matched portion.
If at any point there is a mismatch, then we wouldn't have to re-check all the previous characters, we would just go to one position after the start point, that would be our new start point.
For example, when processing the given example, we would eventually set start to index 4 (0 based index), and match ab, then set it to index 6 and match ab again, then set index to 8. At that point there would be a mismatch, and we would just start matching again at index 9. There is no need to start over from index 5.
This is my guess, and it appears to work, but I can't seem to prove why this works. Can someone help me understand why this works? Or doesn't work?
You algorithm as described doesn't work.
Consider $s=baab$ and $I=\textrm{baabaabbaab}$. Your algorithm would match $s$ with the first $4$ characters of $I$, and since the next $4$ characters don't match $s$, it would then continue with searching $s$ in $I' = aabbaab$.
There is only one occurrence of $s$ in $I'$ causing your algorithm to return $1$ (while the correct answer is $2$).
Here is a solution for your problem that runs in $O(|I|)$ worst-case time.
We can safely assume $|s| \le |I|$, as otherwise the answer is trivially $0$.
Step 1. Construct a DFA $D$ for the language of all words that end with $s$.
This DFA is essentially a path labelled with the characters in $s$ where the last state is a final state. Additionally, each state has &quot;back-edges&quot; that handle characters different from the (at most one) &quot;forward&quot; edge in the path. These &quot;back-edges&quot; point to the state representing the larger prefix of $s$ that is also a suffix of the string read so far.
This sounds more complicated than it really is, and can be computed in linear time (for any constant-size alphabet). Here is an example for $s=\textrm{abba}$ on the alphabet $\{a,b\}$.

Step 2. Now now create an array $A[1, \dots, |I|]$, and feed each character of $I$ to $D$. Whenever $D$ is in the final state after reading the $i$-th character of $I$ set $A[i]$ to $1$, otherwise set $A[i]$ to $0$.
Step 3. Compute the maximum number of consecutive occurrences of $s$ in $I$ using dynamic programming and the array $A$.
Define $B[i]$ as the maximum number of contiguous occurrences of $s$ in the prefix $I_i$ consisting of the first $i$ character of $I$, with the additional constraint that the last occurrence in the contiguous sequence must end with the last character of $I_i$.
For $i = 0, 1, \dots, |s|-1$ we have $B[i]=0$, while for $i = |s|, \dots, |I|$ we can write:
$$
B[i] = \begin{cases} 
0 &amp; \mbox{if $A[i]=0$} \\
1 + B[i-|s|] &amp;  \mbox{if $A[i]=1$} 
\end{cases}
= A[i](1+B[i-|s|]).
$$
Then the solution to your problem is then $\ell = \max_{i=0, \dots, |I|} B[i]$ and the $\ell$ consecutive occurrences of $s$ start at character number $\arg \max_{i=0, \dots, |I|} B[i] - \ell |s|$  in $I$.
A naive implementation of the dynamic programming algorithm requires time $O(|I|)$ and space $O(|I|)$.
However, one can observe that the value stored in $B[i]$ is useless after $B[i+|s|]$ has been computed (except for finding $\ell$).
Then, we can replace $B$ with a circular array of length $|s|$ (so that each access to $B[i]$ becomes an access to $B[i \bmod |s|]$) and compute $\ell$ by keeping track of the maximum values of $B[i]$ encountered so far.
Moreover, the array $A$ is not needed as we can run Step 2 and Step 3 in parallel: we feed the $i$-th character of $I$ to $D$ and immediately compute $B[i \bmod |s|]$ depending on whether $D$ is in the final state or not.
With these modifications the algorithm requires $O(|I|)$ time and $O(|s|)$ space.
Finally, notice that we don't really need to explicitly represent $D$: knowing, for each prefix $s'$ of $s$ and each character $c$ of the alphabet, the length of the longest prefix of $s$ that is a suffix of $s \circ c$ is enough (where $\circ$ denotes concatenation). See this chapter from Jeff Erickson's book Algorithms for a way to efficiently compute these quantities.