|
| 1 | +from functools import reduce |
| 2 | +from typing import List |
| 3 | +from unittest import TestCase, main |
| 4 | + |
| 5 | + |
| 6 | +class Solution: |
| 7 | + def encode(self, strs): |
| 8 | + return self.encode_with_base64(strs) |
| 9 | + |
| 10 | + def decode(self, str): |
| 11 | + return self.decode_with_base64(str) |
| 12 | + |
| 13 | + CSV_DELIMITER = ',' |
| 14 | + |
| 15 | + # solve_1. ASCII |
| 16 | + """ |
| 17 | + Runtime: - |
| 18 | + Time Complexity: O(n * m), n은 strs 배열의 길이, m은 strs 배열의 각 문자열들의 평균 길이 |
| 19 | + Space Complexity: O(n * m), n은 strs 배열의 길이, m은 encode/decode된 문자열의 평균 길이 |
| 20 | + Memory: - |
| 21 | + """ |
| 22 | + def encode_with_ascii(self, strs: List[str]) -> str: |
| 23 | + result = [] |
| 24 | + for str in strs: |
| 25 | + encoded_str = reduce( |
| 26 | + lambda acc, cur: acc + cur, |
| 27 | + map(lambda char: f'{ord(char)}'.zfill(3), str) |
| 28 | + ) |
| 29 | + result.append(encoded_str) |
| 30 | + |
| 31 | + return Solution.CSV_DELIMITER.join(result) |
| 32 | + |
| 33 | + def decode_with_ascii(self, str: str) -> List[str]: |
| 34 | + encoded_strs = str.split(Solution.CSV_DELIMITER) |
| 35 | + result = [] |
| 36 | + for encoded_str in encoded_strs: |
| 37 | + decoded_str = '' |
| 38 | + for i in range(0, len(encoded_str), 3): |
| 39 | + chunk = encoded_str[i: i + 3] |
| 40 | + decoded_str += chr(int(chunk)) |
| 41 | + result.append(decoded_str) |
| 42 | + |
| 43 | + return result |
| 44 | + |
| 45 | + # solve_2. Base64 |
| 46 | + """ |
| 47 | + Runtime: - |
| 48 | + Time Complexity: O(n * m), n은 strs 배열의 길이, m은 strs 배열의 각 문자열들의 평균 길이 |
| 49 | + Space Complexity: O(n * m), n은 strs 배열의 길이, m은 encode/decode된 문자열의 평균 길이 |
| 50 | + Memory: - |
| 51 | + """ |
| 52 | + BASE64_CHAR_TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' |
| 53 | + |
| 54 | + def encode_with_base64(self, strs: List[str]) -> str: |
| 55 | + result = [] |
| 56 | + |
| 57 | + for str in strs: |
| 58 | + bin_converted_str = reduce( |
| 59 | + lambda acc, cur: acc + cur, |
| 60 | + map(lambda char: bin(ord(char))[2:].zfill(8), str) |
| 61 | + ) |
| 62 | + |
| 63 | + encoded_str = '' |
| 64 | + for i in range(0, len(bin_converted_str), 6): |
| 65 | + chunk = bin_converted_str[i: i + 6].ljust(6, '0') |
| 66 | + base64_char = Solution.BASE64_CHAR_TABLE[int(chunk, 2)] |
| 67 | + encoded_str += base64_char |
| 68 | + encoded_str.ljust(4 - (len(encoded_str) % 4), '=') |
| 69 | + |
| 70 | + result.append(encoded_str) |
| 71 | + |
| 72 | + return Solution.CSV_DELIMITER.join(result) |
| 73 | + |
| 74 | + def decode_with_base64(self, str: str) -> List[str]: |
| 75 | + result = [] |
| 76 | + |
| 77 | + encoded_strs = str.split(Solution.CSV_DELIMITER) |
| 78 | + for encoded_str in encoded_strs: |
| 79 | + encoded_str = encoded_str.rstrip('=') |
| 80 | + bin_converted_str = reduce( |
| 81 | + lambda acc, cur: acc + cur, |
| 82 | + map(lambda char: bin(Solution.BASE64_CHAR_TABLE.index(char))[2:].zfill(6), encoded_str) |
| 83 | + ) |
| 84 | + |
| 85 | + decoded_str = '' |
| 86 | + for i in range(0, len(bin_converted_str), 8): |
| 87 | + chunk = bin_converted_str[i: i + 8].rjust(8, '0') |
| 88 | + decimal_value = int(chunk, 2) |
| 89 | + if decimal_value != 0: |
| 90 | + decoded_str += chr(decimal_value) |
| 91 | + |
| 92 | + result.append(decoded_str) |
| 93 | + |
| 94 | + return result |
| 95 | + |
| 96 | + |
| 97 | +class _LeetCodeTestCases(TestCase): |
| 98 | + def test_1(self): |
| 99 | + input = ["lint", "code", "love", "you"] |
| 100 | + output = ["lint", "code", "love", "you"] |
| 101 | + solution = Solution() |
| 102 | + encoded = Solution.encode(solution, input) |
| 103 | + decoded = Solution.decode(solution, encoded) |
| 104 | + self.assertEqual(decoded, output) |
| 105 | + |
| 106 | + def test_2(self): |
| 107 | + input = ["we", "say", ":", "yes"] |
| 108 | + output = ["we", "say", ":", "yes"] |
| 109 | + solution = Solution() |
| 110 | + encoded = Solution.encode(solution, input) |
| 111 | + decoded = Solution.decode(solution, encoded) |
| 112 | + self.assertEqual(decoded, output) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + main() |
0 commit comments