We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents b8d3fd1 + cd778a9 commit 77ec919Copy full SHA for 77ec919
python/0535-encode-and-decode-tinyurl.py
@@ -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