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

4444

被折叠的 条评论
为什么被折叠?



