{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c']\n", "['b', 'c', 'a']\n", "['c', 'a', 'b']\n", "['a', 'b', 'c']\n", "['b', 'c']\n", "['c', 'b']\n", "['b']\n", "['b']\n" ] }, { "data": { "text/plain": [ "3" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ---------------------------------------------------------------\n", "# python best courses https://courses.tanpham.org/\n", "# ---------------------------------------------------------------\n", "# Challenge\n", "# Given a string, find the length of the longest substring\n", "# without repeating characters.\n", "\n", "# Examples:\n", "# Given \"abcabcbb\", the answer is \"abc\", which the length is 3.\n", "# Given \"bbbbb\", the answer is \"b\", with the length of 1.\n", "# Given \"pwwkew\", the answer is \"wke\", with the length of 3.\n", "# ---------------------------------------------------------------\n", "# Algorithm\n", "\n", "# In summary : Form all posible sub_strings from original string, then return length of longest sub_string\n", "\n", "# - start from 1st character, form as long as posible sub string\n", "\n", "# - Add first character to sub string\n", "# - Add second character to sub string if second character not exist in sub string\n", "# ...\n", "# - Repeate until got a character which already exist inside sub string or \n", " \n", " \n", "# - start from 2nd character, form as long as posible sub string\n", "\n", "# - Add first character to sub string\n", "# - Add second character to sub string if second character not exist in sub string\n", "# ...\n", "# - Repeate until got a character which already exist inside sub string\n", "\n", "\n", "# ....\n", "\n", "\n", "# - start from final character, form as long as posible sub string\n", "# - Add first character to sub string\n", "# - Add second character to sub string if second character not exist in sub string\n", "# ...\n", "# - Repeate until got a character which already exist inside sub string\n", "# ---------------------------------------------------------------\n", "\n", "str = \"abcbb\"\n", "\n", "def longest_non_repeat(str):\n", " \n", " # init start position and max length \n", " i=0\n", " max_length = 1\n", "\n", " for i,c in enumerate(str):\n", "\n", " # init counter and sub string value \n", " start_at = i\n", " sub_str=[]\n", "\n", " # continue increase sub string if did not repeat character \n", " while (start_at < len(str)) and (str[start_at] not in sub_str):\n", " sub_str.append(str[start_at])\n", " start_at = start_at + 1\n", "\n", " # update the max length \n", " if len(sub_str) > max_length:\n", " max_length = len(sub_str)\n", "\n", " print(sub_str)\n", " \n", " return max_length\n", "\n", "longest_non_repeat(str)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }