1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| class Codec:
def __init__(self):
# Using a list to keep track of original URLs.
self.urls = []
def encode(self, longUrl: str) -> str:
"""Encodes a URL to a shortened URL.
"""
# Append the longUrl to the list and use the index as the tiny URL.
self.urls.append(longUrl)
return str(len(self.urls) - 1)
def decode(self, shortUrl: str) -> str:
"""Decodes a shortened URL to its original URL.
"""
# Convert the tiny URL back to an index and return the corresponding longUrl.
return self.urls[int(shortUrl)]
|