Combinations

I am trying to write a program that will write out all of the permutations of some letters. Say I want two letters and two of each letter: AABB. I want to get the 6 different ways to write those (AABB, ABAB, ABBA, BAAB, BABA, BBAA).

My first thoughts were to write a nested for loop with a for loop for each place a letter goes. But this wouldn't scale very well, as I want the program to be able to work for different numbers of letters.

I had some other ideas, but none have gone very far yet. Any suggestions?