Print all combinations of a string(assume that duplicates are allowed)

If there are 'n' elements and we have to choose 'r' from them (assuming each element's quantity is unlimited),
then each element can be chosen in 'n' ways, so 'r' elements can be chosen in
nxnx ... n = nr times.
Java code:

We could have also used recursion to achieve the same thing.
An index would be incremented in each recursive call and recursion would terminate when the index became >= r
And in every recursive call, we would iterate from 0 to n to choose a character for the indexth position.

You might be tempted to do this using two loops - one running from 0 to r and the other from 0 to n
However, that will generate only rxn combinations and would be incorrect.