I want to generate all possible strings of a certain length l given n characters.
So for example if l = 2 and the characters are [a,b,c] the result should be: 'aa', 'ab', 'ac', 'ba', 'bb','bc', 'ca', 'cb' and 'cc'.
Initially I had done this using a recursive algorithm that would do this. However, I get a lot of duplicates and it is very time consuming.
Moreover, I thought about an algorithm that would "count" in base n. So, in the example above if I replace a->0, b->1 and c->2 I am effectively counting to 'cc'-> 22 in base 3. However, this also strikes me as inefficient.
Any suggestions ?