Recently I am training for an algorithm contest. One of the older textbooks for that contest contains this question:
Generate all strings $S$ that contained the letters $\begin{Bmatrix}A, B, C\end{Bmatrix}$ only, and satisfy these requirements:
- The string's length (number of characters) must be exactly 100
- No two adjacent strings are the same (formally this means that no two adjacent characters are the same and for all triplets $(a, b, c)$ such that $a \leq b < b + 1 \leq c \leq 100$, $S[a..b] \neq S[b+1..c]$ with $S[x..y]$ taken to be the substring from the $x$th letter to the $y$th letter
- The letter 'B' must be used at few times at possible
I have an algorithm in mind, however it has time complexity of $O(3^n . n^3)$, which is too large for the requirement of $n = 100$. Do you have any suggestions or pieces of example code? Thanks for your help!