Give all possible combinations of a string without duplicates in c++. Example input: \"123\" and output combinations will be:
1,12,123,13,2,23,3.
What the OP is looking for is equivalent to the Power Set minus the Empty Set. The desired output can easily be achieved without recursion. Here is a simple approach:
#include
#include
#include
#include
void GetPowerSet(std::string v) {
std::string emptyString;
std::vector powerSet;
int n = (int) std::pow(2.0, (double) v.size()); // Get size of power set of v
powerSet.reserve(n);
powerSet.push_back(emptyString); // add empty set
for (std::string::iterator it = v.begin(); it < v.end(); it++) {
unsigned int tempSize = powerSet.size();
for (std::size_t j = 0; j < tempSize; j++)
powerSet.push_back(powerSet[j] + *it);
}
// remove empty set element
powerSet.erase(powerSet.begin());
// print out results
std::cout << "Here is your output : ";
for (std::vector::iterator it = powerSet.begin(); it < powerSet.end(); it++)
std::cout << *it << ' ';
}
int main() {
std::string myStr;
std::cout << "Please enter a string : ";
std::cin >> myStr;
GetPowerSet(myStr);
return 0;
}
Here is the output:
Please enter a string : 123
Here is your output : 1 2 12 3 13 23 123
We first note that the size of the power set is given by 2^n
where n
is the size of the initial set. For our purposes, our final vector will only contain 2^n - 1
elements, however we still need to reserve 2^n
in order to prevent resizing as the "empty" element is needed to construct our result.
The real work is carried out inside the two for loops
in the middle of GetPowerSet
. We start with a blank element. We then iterate over each character in our original vector creating subsets of our power set along the way. E.g
powerSet = {}
v
to every element of the power set above: '' + '1' = '1'
.
powerSet = {{}, '1'}
v
to every element of the power set above: '' + '2' = '2', '1' + '2' = '12'
powerSet = {{}, '1', '2', '12'}
v
to every element of the power set above: '' + '3' = '3', '1' + '3' = '13', '2' + '3' = '23', '12' + '3' = '123'
powerSet = {{}, '1', '2', '12', '3', '13', '23', '123'}
powerSet = {'1', '2', '12', '3', '13', '23', '123'}
And we're done.