"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: Given a magazine, see if a ransom note could have been written using the letters in the magazine.\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",
"* Is this case sensitive?\n",
" * Yes\n",
"* Can we assume we're working with ASCII characters?\n",
" * Yes\n",
"* Can we scan the entire magazine, or should we scan only when necessary?\n",
" * You can scan the entire magazine\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",
"* None -> Exception\n",
"* '', '' -> Exception\n",
"* 'a', 'b' -> False\n",
"* 'aa', 'ab' -> False\n",
"* 'aa', 'aab' -> True"
]
},
{
"cell_type":"markdown",
"metadata":{},
"source":[
"## Algorithm\n",
"\n",
"* Build a dictionary of the magazine characters and counts.\n",
"* Loop through each letter in the ransom note and see if there are enough letters in the magazine's dictionary.\n",
"* Note: You could make this more efficient by not scanning the entire magazine all at once, but instead scan just in time as you run out of letters in the dictionary.\n",
"\n",
"Complexity:\n",
"* Time: O(n+m), where n is the length of the ransom note and m is the length of the magazine\n",