I have an array containing repeating characters. Take
['A','B','C','C']
as example. I am writing a C++ program to output all the combinations of size N.
When N = 2, program should output AB, AC, BC and CC.
When N = 3, program should output ABC, ACC, BCC.
My approach was to treat each character of the array as unique. I generated the all combinations and saved it to a vector. I then iterated through the vector to remove duplicate combinations.
For N = 2, assuming all characters are unique gives 4C2 = 6 possibilities : AB, AC, AC, BC, BC, CC.
1 AC and 1 BC are removed because they occur twice instead of once.
Is there a more efficient way to do this?
Note:
- The characters in the array are not necessarily in ascending order.
- In my initial approach, using find() for vector was insufficient to locate all duplicates. For array
['T','O','M','O','R','R','O','W']
, the duplicates are permuted. For N = 4, two of the duplicates are TOMO and TMOO.