Oh no!!!! I like these challenges too much!!!
My two cents:
K: the number of used keys (will call them A,B,C....)
L: the sequence length
NC: The number of possible combinations (not compressed) NC = K^L (power)
So, the "brute force" sequence (all combinations one after the other) would have L*(K^L) digits
A lower-bound is (k^L)+L-1 (if no sequences are repeated in the "compressed" string)
For the trivial case, L=2 we have
K=2, L=2 ---> NC= 2^2=4 --> minimum sequence length = 5 (AABBA)
K=3, L=2 ---> NC= 3^2=9 --> minimum sequence length = 10 (AABBA-CCBCA)
K=4, L=2 ---> NC= 4^2=16--> minimum sequence length = 17 (AABBACCBCA-DDCDBDA)
K=5, L=2 ---> NC= 5^2=25--> minimum sequence length = 26 (AABBACCBCADDBDCDA-EEDECEBEA)
So, it "seems" that for L=2, the minimum sequence length is (K^2)+1 --> which is the general lower bound
Also, for L=2, it is easy to generate the string recursively from (K,L) to (k+1,L)
If we could find the way to generate the string recursivelly from (K,L) to (K,L+1) then the algorithm would be done!
----------------EDIT----------------
Uhm! I see a difference which makes what I wrote above not to describe exactly what you were asking for
You say that if there are 2 keys, both of them are used at least once. That's why 0000 and 1111 are not valid (14 combinations instead of 16, which would be if you could can choose to use or not the allowed keys) .
So, for a general case with K keys and length=L this would mean that each key can appear repeatedly in the sequence at most L+1-K times