-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
29 lines (23 loc) · 737 Bytes
/
solution.py
File metadata and controls
29 lines (23 loc) · 737 Bytes
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
class Codec:
def __init__(self):
self.index = 0
self.map = {}
def encode(self, longUrl):
"""Encodes a URL to a shortened URL.
:type longUrl: str
:rtype: str
"""
self.map[self.index] = longUrl
temp = self.index
self.index += 1
return 'http://tinyurl.com/' + str(temp)
def decode(self, shortUrl):
"""Decodes a shortened URL to its original URL.
:type shortUrl: str
:rtype: str
"""
i = shortUrl.replace('http://tinyurl.com/', '')
return self.map.get(int(i))
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))