Skip to content

Instantly share code, notes, and snippets.

@itayB
Created September 4, 2016 17:19
Show Gist options
  • Select an option

  • Save itayB/87486270b0e9b217aaa1fbfe9a1c8860 to your computer and use it in GitHub Desktop.

Select an option

Save itayB/87486270b0e9b217aaa1fbfe9a1c8860 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<char*>* powerSet(char* src) {
if (strlen(src) == 0) {
vector<char*>* ret = new vector<char*>();
ret->push_back("");
return ret;
}
char digit = src[0];
src++;
vector<char*>* res1 = powerSet(src);
vector<char*>* res2 = new vector<char*>();
for (int i=0 ; i < res1->size() ; i++) {
char* str = new char[strlen(res1->at(i))+1];
str[0] = digit;
// duplicate
strcpy(str+1, res1->at(i));
res2->push_back(str);
}
// concatenating vectors
res1->insert( res1->end(), res2->begin(), res2->end() );
return res1;
}
int main() {
vector<char*>* res = powerSet("abc");
for (int i=0 ; i < res->size() ; i++) {
cout << res->at(i) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment