Add longest substr k distinct challenge

This commit is contained in:
Donne Martin 2017-03-29 04:33:18 -04:00
parent 5975d3f9a8
commit 0cd05ebe36
4 changed files with 400 additions and 0 deletions

View File

@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Find the length of the longest substring with at most k distinct characters.\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume the strings are ASCII?\n",
" * Yes\n",
"* Is this case sensitive?\n",
" * Yes\n",
"* Is a substring a contiguous block of chars?\n",
" * Yes\n",
"* Do we expect an int as a result?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> TypeError\n",
"* '', k = 3 -> 0\n",
"* 'abcabcdefgghiij', k=3 -> 6\n",
"* 'abcabcdefgghighij', k=3 -> 7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def longest_substr(self, string, k):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# %load test_longest_substr.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestSolution(object):\n",
"\n",
" def test_longest_substr(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.longest_substr, None)\n",
" assert_equal(solution.longest_substr('', k=3), 0)\n",
" assert_equal(solution.longest_substr('abcabcdefgghiij', k=3), 6)\n",
" assert_equal(solution.longest_substr('abcabcdefgghighij', k=3), 7)\n",
" print('Success: test_longest_substr')\n",
"\n",
"\n",
"def main():\n",
" test = TestSolution()\n",
" test.test_longest_substr()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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: Find the length of the longest substring with at most k distinct characters.\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",
"* Can we assume the inputs are valid?\n",
" * No\n",
"* Can we assume the strings are ASCII?\n",
" * Yes\n",
"* Is this case sensitive?\n",
" * Yes\n",
"* Is a substring a contiguous block of chars?\n",
" * Yes\n",
"* Do we expect an int as a result?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* None -> TypeError\n",
"* '', k = 3 -> 0\n",
"* 'abcabcdefgghiij', k=3 -> 6\n",
"* 'abcabcdefgghighij', k=3 -> 7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We'll use a `chars_to_index_map` dictionary: char (key) to index (val) map to maintain a sliding window.\n",
"\n",
"The index (val) will keep track of the character index in the input string.\n",
"\n",
"For each character in the string:\n",
"\n",
"* Add the char (key) and index (value) to the map\n",
"* If the length of our map is greater than k, then we'll need to eliminate one item\n",
" * Scan the map to find the lowest index and remove it\n",
" * The new lowest index will therefore be incremented by 1\n",
"* The max length will be the current index minus the lower index + 1\n",
"\n",
"Complexity:\n",
"* Time: O(n * k), where n is the number of chars, k is the length of the map due to the min() call\n",
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Solution(object):\n",
"\n",
" def longest_substr(self, string, k):\n",
" if string is None:\n",
" raise TypeError('string cannot be None')\n",
" if k is None:\n",
" raise TypeError('k cannot be None')\n",
" low_index = 0\n",
" max_length = 0\n",
" chars_to_index_map = {}\n",
" for index, char in enumerate(string):\n",
" chars_to_index_map[char] = index\n",
" if len(chars_to_index_map) > k:\n",
" low_index = min(chars_to_index_map.values())\n",
" del chars_to_index_map[string[low_index]]\n",
" low_index += 1\n",
" max_length = max(max_length, index - low_index + 1)\n",
" return max_length"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_longest_substr.py\n"
]
}
],
"source": [
"%%writefile test_longest_substr.py\n",
"from nose.tools import assert_equal, assert_raises\n",
"\n",
"\n",
"class TestSolution(object):\n",
"\n",
" def test_longest_substr(self):\n",
" solution = Solution()\n",
" assert_raises(TypeError, solution.longest_substr, None)\n",
" assert_equal(solution.longest_substr('', k=3), 0)\n",
" assert_equal(solution.longest_substr('abcabcdefgghiij', k=3), 6)\n",
" assert_equal(solution.longest_substr('abcabcdefgghighij', k=3), 7)\n",
" print('Success: test_longest_substr')\n",
"\n",
"\n",
"def main():\n",
" test = TestSolution()\n",
" test.test_longest_substr()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success: test_longest_substr\n"
]
}
],
"source": [
"%run -i test_longest_substr.py"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

View File

@ -0,0 +1,21 @@
from nose.tools import assert_equal, assert_raises
class TestSolution(object):
def test_longest_substr(self):
solution = Solution()
assert_raises(TypeError, solution.longest_substr, None)
assert_equal(solution.longest_substr('', k=3), 0)
assert_equal(solution.longest_substr('abcabcdefgghiij', k=3), 6)
assert_equal(solution.longest_substr('abcabcdefgghighij', k=3), 7)
print('Success: test_longest_substr')
def main():
test = TestSolution()
test.test_longest_substr()
if __name__ == '__main__':
main()