From c9510e71b629c3d18a5c3bfc6dc0e25967234692 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 28 Mar 2017 05:01:34 -0400 Subject: [PATCH] Add longest substring k distinct challenge --- .../longest_substr_k_distinct/__init__.py | 0 .../longest_substr_challenge.ipynb | 171 ++++++++++++++ .../longest_substr_solution.ipynb | 208 ++++++++++++++++++ .../test_longest_substr.py | 21 ++ 4 files changed, 400 insertions(+) create mode 100644 recursion_dynamic/longest_substr_k_distinct/__init__.py create mode 100644 recursion_dynamic/longest_substr_k_distinct/longest_substr_challenge.ipynb create mode 100644 recursion_dynamic/longest_substr_k_distinct/longest_substr_solution.ipynb create mode 100644 recursion_dynamic/longest_substr_k_distinct/test_longest_substr.py diff --git a/recursion_dynamic/longest_substr_k_distinct/__init__.py b/recursion_dynamic/longest_substr_k_distinct/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recursion_dynamic/longest_substr_k_distinct/longest_substr_challenge.ipynb b/recursion_dynamic/longest_substr_k_distinct/longest_substr_challenge.ipynb new file mode 100644 index 0000000..0636cdd --- /dev/null +++ b/recursion_dynamic/longest_substr_k_distinct/longest_substr_challenge.ipynb @@ -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 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.5.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/recursion_dynamic/longest_substr_k_distinct/longest_substr_solution.ipynb b/recursion_dynamic/longest_substr_k_distinct/longest_substr_solution.ipynb new file mode 100644 index 0000000..bd71b1f --- /dev/null +++ b/recursion_dynamic/longest_substr_k_distinct/longest_substr_solution.ipynb @@ -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 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 not isinstance(string, str):\n", + " raise TypeError('string must be of type str')\n", + " if not isinstance(k, int):\n", + " raise TypeError('k must be of type int')\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 +} diff --git a/recursion_dynamic/longest_substr_k_distinct/test_longest_substr.py b/recursion_dynamic/longest_substr_k_distinct/test_longest_substr.py new file mode 100644 index 0000000..6410c7f --- /dev/null +++ b/recursion_dynamic/longest_substr_k_distinct/test_longest_substr.py @@ -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() \ No newline at end of file