#!/usr/bin/env python # coding: utf-8 # 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). # # Challenge Notebook # ## Problem: Find the length of the longest substring with at most k distinct characters. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Can we assume the inputs are valid? # * No # * Can we assume the strings are ASCII? # * Yes # * Is this case sensitive? # * Yes # * Is a substring a contiguous block of chars? # * Yes # * Do we expect an int as a result? # * Yes # * Can we assume this fits memory? # * Yes # ## Test Cases # # * None -> TypeError # * '', k = 3 -> 0 # * 'abcabcdefgghiij', k=3 -> 6 # * 'abcabcdefgghighij', k=3 -> 7 # ## Algorithm # # 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. # ## Code # In[ ]: class Solution(object): def longest_substr(self, string, k): # TODO: Implement me pass # ## Unit Test # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_longest_substr.py import unittest class TestSolution(unittest.TestCase): def test_longest_substr(self): solution = Solution() self.assertRaises(TypeError, solution.longest_substr, None) self.assertEqual(solution.longest_substr('', k=3), 0) self.assertEqual(solution.longest_substr('abcabcdefgghiij', k=3), 6) self.assertEqual(solution.longest_substr('abcabcdefgghighij', k=3), 7) print('Success: test_longest_substr') def main(): test = TestSolution() test.test_longest_substr() if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook]() for a discussion on algorithms and code solutions.