[NeetCode150] String Encode and Decode

String Encode and Decode

Design an algorithm to encode a list of strings to a single string. The encoded string is then decoded back to the original list of strings.

Please implement encode and decode

Example 1:

Input: [“neet”,“code”,“love”,“you”]

Output:[“neet”,“code”,“love”,“you”]

Example 2:

Input: [“we”,“say”,“:”,“yes”]

Output: [“we”,“say”,“:”,“yes”]

Constraints:

0 <= strs.length < 100
0 <= strs[i].length < 200
strs[i] contains only UTF-8 characters.

Solution

The constraints say that “strs[i] contains only UTF-8 characters”, so it is obvious to think about converting characters into their corresponding hex code, which only contains char 0-9 and a-z. Once we convert them into hex code, we can easily put separating symbol, such as , between each string to merge them into a single string, and then split them apart by our pre-defined separating symbol.

In Python, to convert one string into bytes type, we need str.encode("utf-8"), then we use bytes.hex() convert the bytes into hex form and use str() to save them as string (If you do not do so, ",".join(bytes) will lead to a error).

To decode, just split the string by str.split() first, then convert hex string into bytes by bytes.fromhex(). Finally, use bytes.decode("utf-8") converting the bytes back to string.

Special Notice:
The test data contains edging situation like [](empty list) and [""] empty string. In my case, I specially deal with the former situation.

Full code:

class Solution:
    def encode(self, strs: List[str]) -> str:
        def encode_one_word(word: str) -> str:
            utf8_bytes = word.encode("utf-8")
            hex_rep = utf8_bytes.hex()
            return str(hex_rep)
        if len(strs) == 0:
            return "EMPTY"
        encoded_list = []
        for string in strs:
            encoded_list.append(encode_one_word(string))
        return ",".join(encoded_list)

    def decode(self, s: str) -> List[str]:
        if s == "EMPTY":
            return []
        encoded_list = s.split(",")
        decoded_list = []
        for code in encoded_list:
            utf8_bytes = bytes.fromhex(code)
            decoded_list.append(utf8_bytes.decode('utf-8'))
        return decoded_list

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值