mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-02 23:48:02 +00:00
203 lines
5.1 KiB
Python
203 lines
5.1 KiB
Python
{
|
|
"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": {},
|
|
"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": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Overwriting test_longest_substr.py\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%writefile test_longest_substr.py\n",
|
|
"import unittest\n",
|
|
"\n",
|
|
"\n",
|
|
"class TestSolution(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_longest_substr(self):\n",
|
|
" solution = Solution()\n",
|
|
" self.assertRaises(TypeError, solution.longest_substr, None)\n",
|
|
" self.assertEqual(solution.longest_substr('', k=3), 0)\n",
|
|
" self.assertEqual(solution.longest_substr('abcabcdefgghiij', k=3), 6)\n",
|
|
" self.assertEqual(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": {},
|
|
"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.7.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|