"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"# Solution Notebook"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Problem: Return all subsets of a set.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Constraints\n",
"\n",
"* Should the resulting subsets be unique?\n",
" * Yes, treat 'ab' and 'bc' as the same\n",
"* Is the empty set included as a subset?\n",
" * Yes\n",
"* Are the inputs unique?\n",
" * No\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Test Cases\n",
"\n",
"<pre>\n",
"* None -> None\n",
"* '' -> ['']\n",
"* 'a' -> ['a', '']\n",
"* 'ab' -> ['a', 'ab', 'b', '']\n",
"* 'abc' -> ['a', 'ab', 'abc', 'ac',\n",
"'b', 'bc', 'c', '']\n",
"* 'aabc' -> ['a', 'aa', 'aab', 'aabc', \n",
"'aac', 'ab', 'abc', 'ac', \n",
"'b', 'bc', 'c', '']\n",
"</pre>"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"* Build a dictionary of {chars: counts} where counts is the number of times each char is found in the input\n",
"* Loop through each item in the dictionary\n",
" * Keep track of the current index (first item will have current index 0)\n",
" * If the char's count is 0, continue\n",
" * Decrement the current char's count in the dictionary\n",
" * Add the current char to the current results\n",
" * Add the current result to the results\n",
" * Recurse, passing in the current index as the new starting point index\n",
" * When we recurse, we'll check if current index < starting point index, and if so, continue\n",
" * This avoids duplicate results such as 'ab' and 'bc'\n",
" * Backtrack by:\n",
" * Removing the just added current char from the current results\n",