Skip to content

Commit 3f20d6d

Browse files
committed
Encode and Decode TinyURL
1 parent 5776c83 commit 3f20d6d

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

.ipynb_checkpoints/Hash-checkpoint.ipynb

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,72 @@
119119
"print(isAnagram(\"rat\",\"car\"))"
120120
]
121121
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 7,
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"name": "stdout",
129+
"output_type": "stream",
130+
"text": [
131+
"http://tinyurl.com/01234\n",
132+
"https://leetcode.com/problems/design-tinyurl\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"\"\"\"\n",
138+
"535. Encode and Decode TinyURL\n",
139+
"\n",
140+
"Note: This is a companion problem to the System Design problem: Design TinyURL.\n",
141+
"\n",
142+
"TinyURL is a URL shortening service where you enter a URL such as \n",
143+
"https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.\n",
144+
"\n",
145+
"Design the encode and decode methods for the TinyURL service. \n",
146+
"There is no restriction on how your encode/decode algorithm should work.\n",
147+
"You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.\n",
148+
"\"\"\"\n",
149+
"\n",
150+
"class Codec:\n",
151+
"\n",
152+
" Hash = dict()\n",
153+
"\n",
154+
" def encode(self, longUrl: str) -> str:\n",
155+
" arr = longUrl.split(\"/\")\n",
156+
"# print(arr)\n",
157+
" url = \"\"\n",
158+
" for x in range(0,len(arr)):\n",
159+
"# print(x)\n",
160+
" self.Hash[x] = arr[x]\n",
161+
" url += str(x)\n",
162+
" \n",
163+
" return \"http://tinyurl.com/\"+url\n",
164+
"\n",
165+
" def decode(self, shortUrl: str) -> str:\n",
166+
" \"\"\"Decodes a shortened URL to its original URL.\n",
167+
" \"\"\"\n",
168+
"# print(shortUrl,self.Hash)\n",
169+
" short = shortUrl.split('http://tinyurl.com/')\n",
170+
" url = \"\"\n",
171+
" for x in short[1]:\n",
172+
" url += self.Hash[int(x)]\n",
173+
" if(x!=short[1][-1]):\n",
174+
" url+=\"/\"\n",
175+
" \n",
176+
" print(url)\n",
177+
" return url\n",
178+
" \n",
179+
" \n",
180+
"\n",
181+
"codec = Codec()\n",
182+
"# codec.decode(codec.encode(\"https://leetcode.com/problems/design-tinyurl\"))\n",
183+
"encodedUrl = codec.encode(\"https://leetcode.com/problems/design-tinyurl\")\n",
184+
"print(encodedUrl)\n",
185+
"decodedUrl = codec.decode(encodedUrl)"
186+
]
187+
},
122188
{
123189
"cell_type": "code",
124190
"execution_count": null,
@@ -143,7 +209,7 @@
143209
"name": "python",
144210
"nbconvert_exporter": "python",
145211
"pygments_lexer": "ipython3",
146-
"version": "3.6.10"
212+
"version": "3.7.4"
147213
}
148214
},
149215
"nbformat": 4,

Hash.ipynb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,72 @@
119119
"print(isAnagram(\"rat\",\"car\"))"
120120
]
121121
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 7,
125+
"metadata": {},
126+
"outputs": [
127+
{
128+
"name": "stdout",
129+
"output_type": "stream",
130+
"text": [
131+
"http://tinyurl.com/01234\n",
132+
"https://leetcode.com/problems/design-tinyurl\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"\"\"\"\n",
138+
"535. Encode and Decode TinyURL\n",
139+
"\n",
140+
"Note: This is a companion problem to the System Design problem: Design TinyURL.\n",
141+
"\n",
142+
"TinyURL is a URL shortening service where you enter a URL such as \n",
143+
"https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk.\n",
144+
"\n",
145+
"Design the encode and decode methods for the TinyURL service. \n",
146+
"There is no restriction on how your encode/decode algorithm should work.\n",
147+
"You just need to ensure that a URL can be encoded to a tiny URL and the tiny URL can be decoded to the original URL.\n",
148+
"\"\"\"\n",
149+
"\n",
150+
"class Codec:\n",
151+
"\n",
152+
" Hash = dict()\n",
153+
"\n",
154+
" def encode(self, longUrl: str) -> str:\n",
155+
" arr = longUrl.split(\"/\")\n",
156+
"# print(arr)\n",
157+
" url = \"\"\n",
158+
" for x in range(0,len(arr)):\n",
159+
"# print(x)\n",
160+
" self.Hash[x] = arr[x]\n",
161+
" url += str(x)\n",
162+
" \n",
163+
" return \"http://tinyurl.com/\"+url\n",
164+
"\n",
165+
" def decode(self, shortUrl: str) -> str:\n",
166+
" \"\"\"Decodes a shortened URL to its original URL.\n",
167+
" \"\"\"\n",
168+
"# print(shortUrl,self.Hash)\n",
169+
" short = shortUrl.split('http://tinyurl.com/')\n",
170+
" url = \"\"\n",
171+
" for x in short[1]:\n",
172+
" url += self.Hash[int(x)]\n",
173+
" if(x!=short[1][-1]):\n",
174+
" url+=\"/\"\n",
175+
" \n",
176+
" print(url)\n",
177+
" return url\n",
178+
" \n",
179+
" \n",
180+
"\n",
181+
"codec = Codec()\n",
182+
"# codec.decode(codec.encode(\"https://leetcode.com/problems/design-tinyurl\"))\n",
183+
"encodedUrl = codec.encode(\"https://leetcode.com/problems/design-tinyurl\")\n",
184+
"print(encodedUrl)\n",
185+
"decodedUrl = codec.decode(encodedUrl)"
186+
]
187+
},
122188
{
123189
"cell_type": "code",
124190
"execution_count": null,

0 commit comments

Comments
 (0)