File size: 4,006 Bytes
ce5cd7e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
{
"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 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|