{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Number 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://leetcode.com/problems/longest-substring-without-repeating-characters/" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "class Solution:\n", " def lengthOfLongestSubstring(self, s: str) -> int:\n", " i1 = 0\n", " s_list = list(s)\n", " \n", " len_s = len(s_list)\n", " max_string_size = 0\n", "\n", " for i1 in range(len_s):\n", " print(i1)\n", " char_list = [] # check if character already in list\n", " for i2 in range(i1, len_s):\n", " cur_char = s_list[i2]\n", " if cur_char not in char_list:\n", " char_list.append(cur_char)\n", " else:\n", " # Time to recalculate\n", " break\n", " cur_string_size = len(char_list) \n", " max_string_size = cur_string_size if cur_string_size > max_string_size else max_string_size\n", "\n", " return max_string_size\n", "\n", " \n", "\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n" ] }, { "data": { "text/plain": [ "6" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Solution()\n", "s.lengthOfLongestSubstring(\"abhiram\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Let's see if this can be faster**\n", "\n", "Update - This actually threw a TLE on a really long input. Fail. ⛔️" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "class Solution2:\n", " def lengthOfLongestSubstring2(self, s: str) -> int:\n", " i1 = 0\n", " s_list = list(s)\n", " \n", " len_s = len(s_list)\n", " max_string_size = 0\n", "\n", " for i1 in range(len_s):\n", " for i2 in range(len_s, i1-1,-1):\n", " cur_word = s_list[i1:i2]\n", " len_set = len(set(cur_word))\n", " len_list = len((cur_word))\n", " if len_set < len_list:\n", " pass\n", " else:\n", " cur_string_size = len(cur_word) \n", " max_string_size = cur_string_size if cur_string_size > max_string_size else max_string_size\n", " break\n", "\n", " return max_string_size" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Solution2()\n", "s.lengthOfLongestSubstring2(\"abhiram\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Same double pointer trick, but this time start from the reverse**" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "class Solution3:\n", " def lengthOfLongestSubstring3(self, s: str) -> int:\n", " i1 = 0\n", " s_list = list(s)\n", " \n", " len_s = len(s_list)\n", " max_string_size = 0\n", "\n", " for i1 in range(len_s):\n", " char_list = [] # check if character already in list\n", " if max_string_size > len_s - i1:\n", " break\n", " for i2 in range(i1, len_s):\n", " cur_char = s_list[i2]\n", " if cur_char not in char_list:\n", " char_list.append(cur_char)\n", " else:\n", " # Time to recalculate\n", " break\n", " cur_string_size = len(char_list) \n", " max_string_size = cur_string_size if cur_string_size > max_string_size else max_string_size\n", " \n", "\n", " return max_string_size\n", "\n", " \n", "\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = Solution3()\n", "s.lengthOfLongestSubstring3(\"abhiram\")" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Solution4:\n", " def lengthOfLongestSubstring4(self, s: str) -> int:\n", " i1 = 0\n", " s_list = list(s)\n", " \n", " len_s = len(s_list)\n", " max_string_size = 0\n", "\n", " for i1 in range(len_s):\n", " char_list = [] # check if character already in list\n", " char_list2 = []\n", " if max_string_size > len_s - i1:\n", " return max_string_size\n", " for i2 in range(i1, len_s):\n", " cur_char = s_list[i2]\n", " if cur_char not in char_list:\n", " char_list.append(cur_char)\n", " else:\n", " # Time to recalculate\n", " break\n", " for i3 in range(i1+1, len_s):\n", " cur_char = s_list[i3]\n", " if cur_char not in char_list2:\n", " char_list2.append(cur_char)\n", " else:\n", " # Time to recalculate\n", " break\n", " i1 = i1+1\n", " cur_string_size = len(char_list) \n", " cur_string_size2 = len(char_list2) \n", " max_string_size = max(max_string_size,cur_string_size,cur_string_size2 )\n", " \n", "\n", " return max_string_size\n", "\n", "s = Solution4()\n", "s.lengthOfLongestSubstring4(\"abhiram\")" ] } ], "metadata": { "interpreter": { "hash": "033d5ea8e9748582193a6d8f975af35153e280c1f566336ac6ff582d76ae2a04" }, "kernelspec": { "display_name": "Python 3.6.8 64-bit", "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.6.8" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }