<p>I know 'best' is subjective, so according to you, what is the best solution for the following problem:</p>

<p>Given a string of length n (say "abc"), generate all proper subsets of the string. So, for our example, the output would be {}, {a}, {b}, {c}, {ab}, {bc}, {ac}. {abc}.</p>

<p>What do you think?</p>

[{'answer_id': 43474, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 0, 'selected': False, 'text': '<pre><code>def subsets(s):\n    r = []\n    a = [False] * len(s)\n    while True:\n        r.append("".join([s[i] for i in range(len(s)) if a[i]]))\n        j = 0\n        while a[j]:\n            a[j] = False\n            j += 1\n            if j &gt;= len(s):\n                return r\n        a[j] = True\n\nprint subsets("abc")\n</code></pre>\n'}, {'answer_id': 43479, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': True, 'text': '<p>You want the <a href="http://en.wikipedia.org/wiki/Power_set" rel="noreferrer">power set</a>. It can be calculated <a href="http://en.wikipedia.org/wiki/Power_set#Algorithms" rel="noreferrer">recursively and inductively</a>. ;-)</p>\n'}, {'answer_id': 43481, 'author': 'Tnilsson', 'author_id': 4165, 'author_profile': 'https://Stackoverflow.com/users/4165', 'pm_score': 0, 'selected': False, 'text': '<p>Pardon the pseudo code...</p>\n\n<pre><code>int i = 0;\nResults.push({});\n\nWhile(i &gt; Inset.Length) {\n   Foreach(Set s in Results) {\n    If(s.Length == i) {\n       Foreach(character c in inSet)\n          Results.push(s+c);\n    }\n    i++;\n}\n</code></pre>\n'}, {'answer_id': 43493, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 2, 'selected': False, 'text': '<p>The recursive approach -- the subsets of "abc" come in two types: those which are subsets of "bc", and those which are "a" plus a subset of "bc". So if you know the subsets of "bc", it\'s easy.</p>\n\n<p>Alternatively, a string of length n has 2^n subsets. So write two nested loops: i counts from 0 to 2^n -1 (for the subsets), and j counts from 0 to n-1 (for characters in the ith subset). Output the jth character of the string if and only if the jth bit of i is 1.</p>\n\n<p>(Well, you did say that "best" was subjective...)</p>\n'}, {'answer_id': 145313, 'author': 'Sarp Centel', 'author_id': 16622, 'author_profile': 'https://Stackoverflow.com/users/16622', 'pm_score': 1, 'selected': False, 'text': '<p>Interpret a number in binary representation as indicating which elements are included in the subset. Let\'s assume that you have 3 elements in your set. Number 4 corresponds to 0100 in binary notation, so you will interpret this as a subset of size 1 that only includes 2nd element. This way, generating all subsets is counting up to (2^n)-1</p>\n\n<pre><code>    char str [] = "abc";\n    int n = strlen(str); // n is number of elements in your set\n\n    for(int i=0; i&lt; (1 &lt;&lt; n); i++) { // (1 &lt;&lt; n) is equal to 2^n\n        for(int j=0; j&lt;n; j++) { // For each element in the set\n            if((i &amp; (1 &lt;&lt; j)) &gt; 0) { // Check if it\'s included in this subset. (1 &lt;&lt; j) sets the jth bit\n                cout &lt;&lt; str[j];\n            }\n        }\n        cout &lt;&lt; endl;\n    }\n</code></pre>\n'}, {'answer_id': 21300716, 'author': 'feliciafay', 'author_id': 1315742, 'author_profile': 'https://Stackoverflow.com/users/1315742', 'pm_score': 0, 'selected': False, 'text': '<pre><code>//recursive solution in C++\nset&lt;string&gt; power_set_recursive(string input_str)\n{\n    set&lt;string&gt; res;\n    if(input_str.size()==0) {\n        res.insert("");\n    } else if(input_str.size()==1) {\n        res.insert(input_str.substr(0,1));\n    } else {\n        for(int i=0;i&lt;input_str.size();i++) {\n            set&lt;string&gt; left_set=power_set_iterative(input_str.substr(0,i));\n            set&lt;string&gt; right_set=power_set_iterative(input_str.substr(i,input_str.size()-i));\n            for(set&lt;string&gt;::iterator it1=left_set.begin();it1!=left_set.end();it1++) {\n                for(set&lt;string&gt;::iterator it2=right_set.begin();it2!=right_set.end();it2++) {\n                    string tmp=(*it1)+(*it2);\n                    sort(tmp.begin(),tmp.end());\n                    res.insert(tmp);\n                }\n            }\n        }\n    }\n    return res;\n}\n\n\n//iterative solution in C++\nset&lt;string&gt; power_set_iterative(string input_str)\n{\n    set&lt;string&gt; res;\n    set&lt;string&gt; out_res;\n    res.insert("");\n    set&lt;string&gt;::iterator res_it;\n    for(int i=0;i&lt;input_str.size();i++){\n        for(res_it=res.begin();res_it!=res.end();res_it++){\n                string tmp=*res_it+input_str.substr(i,1);\n                sort(tmp.begin(),tmp.end());\n                out_res.insert(tmp);\n        }\n        res.insert(input_str.substr(i,1));\n        for(set&lt;string&gt;::iterator res_it2=out_res.begin();res_it2!=out_res.end();res_it2++){\n            res.insert(*res_it2);\n    }\n    out_res.clear();\n    }\n    return res;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43466', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/380/']