Last active
March 6, 2017 12:03
-
-
Save huklee/cf883269e436fe407eaa32d12dbba698 to your computer and use it in GitHub Desktop.
solution : get the all combinations of a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
// bitwise operation solution | |
void solve(string s){ | |
// assert (s.size() <= 32); | |
for (int i=1; i < (1 << s.size()); i++){ | |
string result; | |
for (int j=0; j < s.size(); j++){ | |
if (i & (1 << j)) | |
result += s[j]; | |
} | |
cout << result << endl; | |
} | |
} | |
void rec_solve(string s, string &outstr, int start=0){ | |
for (int i=start; i < s.size(); i++){ | |
outstr += s[i]; | |
cout << outstr << endl; | |
rec_solve(s, outstr, i + 1); | |
outstr.pop_back(); | |
} | |
} | |
// wrapper function | |
void solve2(string s){ | |
string temp; | |
rec_solve(s, temp, 0); | |
} | |
int main(){ | |
solve("asdf"); | |
solve2("asdf"); | |
// SearchZero(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment