#Pyth, 31 bytes
Pyth, 31 bytes
This is the direct conversion of the algorithm used in my CJam answer. Tips for golfing welcome!
Mu?G}H+GG+G>Hefq<HT>G-lGTUH^GHk
This code defines a function g
which takes two arguments, the string of list of characters and the number.
Example usage:
Mu?G}H+GG+G>Hefq<HT>G-lGTUH^GHkg"ABC"3
Output:
AAABAACABBABCACBACCBBBCBCCC
Code expansion:
M # def g(G,H):
u # return reduce(lambda G, H:
?G # (G if
}H # (H in
+GG # add(G,G)) else
+G # add(G,
>H # slice_end(H,
e # last_element(
f # Pfilter(lambda T:
q # equal(
<HT # slice_start(H,T),
>G # slice_end(G,
-lGT # minus(Plen(G),T))),
UH # urange(H)))))),
^GH # cartesian_product(G,H),
k # "")