-
Notifications
You must be signed in to change notification settings - Fork 24
/
535 - Encode and Decode TinyURL.py
41 lines (34 loc) · 1.62 KB
/
535 - Encode and Decode TinyURL.py
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
30
31
32
33
34
35
36
37
38
39
40
41
# Solution 1: Use two hashtables so we can lookup longUrl and shortUrl in O(1)
# To encode we can just increment a counter
# Problems with this approach (creds to StefanPochmann in LeetCode discussions):
# - People can find out how many URLs have already been encoded. Not sure I want them to know.
# - People might try to get special numbers by spamming me with repeated requests shortly before their desired number comes up.
# - Only using digits means the codes can grow unnecessarily large. Only offers a million codes with length 6 (or smaller). Using six digits or lower or upper case letters would offer (10+26*2)6 = 56,800,235,584 codes with length 6.
class Codec:
def __init__(self):
self.count = 0
self.longLookup = {}
self.shortLookup = {}
def encode(self, longUrl):
"""Encodes a URL to a shortened URL.
:type longUrl: str
:rtype: str
"""
if longUrl in self.longLookup:
return self.longLookup[longUrl]
else:
self.longLookup[longUrl] = self.count
self.shortLookup[self.count] = longUrl
self.count += 1
return self.count-1
def decode(self, shortUrl):
"""Decodes a shortened URL to its original URL.
:type shortUrl: str
:rtype: str
"""
return self.shortLookup[shortUrl]
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
# Solution 2: Better to use random strings as keys. If we've generate a random string
# that we've previously encountered, just generate a new one.