728x90
https://leetcode.com/problems/encode-and-decode-tinyurl
import re
import base64
class Codec:
def encode(self, longUrl: str) -> str:
delimeter = '://'
splited = re.split(delimeter, longUrl)
edcoded = base64.urlsafe_b64encode(splited[1].encode('UTF-8')).decode('UTF-8')
short_url = splited[0] + delimeter + edcoded
print(short_url)
return short_url
def decode(self, shortUrl: str) -> str:
delimeter = '://'
splited = re.split(delimeter, shortUrl)
decoded = base64.urlsafe_b64decode(splited[1].encode('UTF-8')).decode('UTF-8')
longUrl = splited[0] + delimeter + decoded
return longUrl
# Your Codec object will be instantiated and called as such:
# codec = Codec()
# codec.decode(codec.encode(url))
앗 통과는 했는데 다시 생각해보니 Base64면 Short URL이 아니겠다 다시 해봐야겠군
728x90
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 1630. Arithmetic Subarrays (0) | 2024.02.20 |
---|---|
[Leetcode] 1656. Design an Ordered Stream (0) | 2024.02.19 |
[Leetcode] 1365. How Many Numbers Are Smaller Than the Current Number (0) | 2024.02.09 |
[Leetcode] 2610. Convert an Array Into a 2D Array With Conditions (0) | 2024.02.08 |
[Leetcode] 1282. Group the People Given the Group Size They Belong To (0) | 2024.02.08 |