I have a string 'UUUUUUUUUDDDDDDD'
those are 9 U
s and 7 D
s.
I want to generate all permutations, like:
'UUUUUUUUUDDDDDDD'
'DUUUUUUUUUDDDDDD'
'DUUUUUDUUUDDDDUD'
'DUUDUUDUUUDDDDUU'
In other words, all possible strings with 9 U
s and 7 D
s
I'm trying to use itertools.permutations()
but that function considers each U
and each D
as a different element so I get repeated permutations.
In the example below I will use a smaller string UUD
just to demonstrate the problem with itertools.permutations
:
for g in itertools.permutations('UUD'):
print(g)
I get
('U', 'U', 'D')
('U', 'D', 'U')
('U', 'U', 'D')
('U', 'D', 'U')
('D', 'U', 'U')
('D', 'U', 'U')
I would want
('U', 'U', 'D')
('U', 'D', 'U')
('D', 'U', 'U')
Generating all possibilites and then adding them to a set to make them unique is not a viable solution, as there will be too many permutations to store in memory. I would like to generate them one at a time, work with it and discard it.