본문 바로가기

IT/코딩테스트

[Leetcode] 535. Encode and Decode TinyURL

728x90

https://leetcode.com/problems/encode-and-decode-tinyurl

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

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