Skip to content

Commit 77ec919

Browse files
authored
Merge pull request neetcode-gh#1739 from AP-Repositories/patch-22
Create 0535-encode-and-decode-tinyurl.py
2 parents b8d3fd1 + cd778a9 commit 77ec919

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Codec:
2+
def __init__(self):
3+
self.encodeMap = {}
4+
self.decodeMap = {}
5+
self.base = "http://tinyurl.com/"
6+
7+
def encode(self, longUrl: str) -> str:
8+
"""Encodes a URL to a shortened URL.
9+
"""
10+
if longUrl not in self.encodeMap:
11+
shortUrl = self.base + str(len(self.encodeMap) + 1)
12+
self.encodeMap[longUrl] = shortUrl
13+
self.decodeMap[shortUrl] = longUrl
14+
return self.encodeMap[longUrl]
15+
16+
def decode(self, shortUrl: str) -> str:
17+
"""Decodes a shortened URL to its original URL.
18+
"""
19+
return self.decodeMap[shortUrl]

0 commit comments

Comments
 (0)