encode_and_decode_strings
1# @leet start 2class Codec: 3 def encode(self, strs: list[str]) -> str: 4 """ 5 This function encodes a list of strings into a singular string. 6 This does this with varchar encoding. Since the string can have any ASCII 7 character in it, it does this by first encoding the length of the string, 8 then a delimiter of some kind (`#` in this case), and then the string. 9 This allows the decoder to first parse digits and recreate the length, 10 Then when it sees the hashtag, it knows to stop parsing digits. 11 Finally, it parses `n` characters, which stops it before the length of the next str. 12 13 Since this problem says that only ASCII characters are allowed, the most efficient 14 way to encode the string would be to use a non-ASCII character as a delimiter, since 15 that only requires one character per string to encode, whereas requires the length 16 of the string in decimal form + a hashtag character. 17 18 However, that approach doesn't work for any arbitrary character, since that 19 could show up in the middle of a str and the tokenizer would cut a string into two 20 that it shouldn't have. Likewise, simply encoding the length doesn't work, because numbers 21 can appear at any time in the string. 22 """ 23 return "".join(f"{len(s)}#{s}" for s in strs) 24 25 def decode(self, s: str) -> list[str]: 26 """ 27 This function decodes the encoded string. The way it does this is by 28 first parsing digits until it doesn't see any digits, and saving that result. 29 Then parsing the hashtag. 30 Then taking that many chars from the string, and adding it to the list of strings. 31 And then advancing the string by that much. 32 """ 33 strs = [] 34 35 while s: 36 num, num_len = self.tok_number(s) 37 s = s[num_len:] 38 self.tok_delimiter(s) 39 s = s[1:] 40 parsed = self.take_chars(num, s) 41 s = s[num:] 42 strs.append(parsed) 43 44 return strs 45 46 def tok_delimiter(self, s: str): 47 if s[0] != "#": 48 raise RuntimeError("The character was not a #") 49 50 def tok_number(self, s: str) -> tuple[int, int]: 51 num = [] 52 for c in s: 53 if c.isdigit(): 54 num.append(c) 55 else: 56 break 57 return (int("".join(num)), len(num)) 58 59 def take_chars(self, n: int, s: str) -> str: 60 return s[:n] 61 62 63# Your Codec object will be instantiated and called as such: 64def test(): 65 assert 2 + 2 == 4
3class Codec: 4 def encode(self, strs: list[str]) -> str: 5 """ 6 This function encodes a list of strings into a singular string. 7 This does this with varchar encoding. Since the string can have any ASCII 8 character in it, it does this by first encoding the length of the string, 9 then a delimiter of some kind (`#` in this case), and then the string. 10 This allows the decoder to first parse digits and recreate the length, 11 Then when it sees the hashtag, it knows to stop parsing digits. 12 Finally, it parses `n` characters, which stops it before the length of the next str. 13 14 Since this problem says that only ASCII characters are allowed, the most efficient 15 way to encode the string would be to use a non-ASCII character as a delimiter, since 16 that only requires one character per string to encode, whereas requires the length 17 of the string in decimal form + a hashtag character. 18 19 However, that approach doesn't work for any arbitrary character, since that 20 could show up in the middle of a str and the tokenizer would cut a string into two 21 that it shouldn't have. Likewise, simply encoding the length doesn't work, because numbers 22 can appear at any time in the string. 23 """ 24 return "".join(f"{len(s)}#{s}" for s in strs) 25 26 def decode(self, s: str) -> list[str]: 27 """ 28 This function decodes the encoded string. The way it does this is by 29 first parsing digits until it doesn't see any digits, and saving that result. 30 Then parsing the hashtag. 31 Then taking that many chars from the string, and adding it to the list of strings. 32 And then advancing the string by that much. 33 """ 34 strs = [] 35 36 while s: 37 num, num_len = self.tok_number(s) 38 s = s[num_len:] 39 self.tok_delimiter(s) 40 s = s[1:] 41 parsed = self.take_chars(num, s) 42 s = s[num:] 43 strs.append(parsed) 44 45 return strs 46 47 def tok_delimiter(self, s: str): 48 if s[0] != "#": 49 raise RuntimeError("The character was not a #") 50 51 def tok_number(self, s: str) -> tuple[int, int]: 52 num = [] 53 for c in s: 54 if c.isdigit(): 55 num.append(c) 56 else: 57 break 58 return (int("".join(num)), len(num)) 59 60 def take_chars(self, n: int, s: str) -> str: 61 return s[:n]
4 def encode(self, strs: list[str]) -> str: 5 """ 6 This function encodes a list of strings into a singular string. 7 This does this with varchar encoding. Since the string can have any ASCII 8 character in it, it does this by first encoding the length of the string, 9 then a delimiter of some kind (`#` in this case), and then the string. 10 This allows the decoder to first parse digits and recreate the length, 11 Then when it sees the hashtag, it knows to stop parsing digits. 12 Finally, it parses `n` characters, which stops it before the length of the next str. 13 14 Since this problem says that only ASCII characters are allowed, the most efficient 15 way to encode the string would be to use a non-ASCII character as a delimiter, since 16 that only requires one character per string to encode, whereas requires the length 17 of the string in decimal form + a hashtag character. 18 19 However, that approach doesn't work for any arbitrary character, since that 20 could show up in the middle of a str and the tokenizer would cut a string into two 21 that it shouldn't have. Likewise, simply encoding the length doesn't work, because numbers 22 can appear at any time in the string. 23 """ 24 return "".join(f"{len(s)}#{s}" for s in strs)
This function encodes a list of strings into a singular string.
This does this with varchar encoding. Since the string can have any ASCII
character in it, it does this by first encoding the length of the string,
then a delimiter of some kind (# in this case), and then the string.
This allows the decoder to first parse digits and recreate the length,
Then when it sees the hashtag, it knows to stop parsing digits.
Finally, it parses n characters, which stops it before the length of the next str.
Since this problem says that only ASCII characters are allowed, the most efficient way to encode the string would be to use a non-ASCII character as a delimiter, since that only requires one character per string to encode, whereas requires the length of the string in decimal form + a hashtag character.
However, that approach doesn't work for any arbitrary character, since that could show up in the middle of a str and the tokenizer would cut a string into two that it shouldn't have. Likewise, simply encoding the length doesn't work, because numbers can appear at any time in the string.
26 def decode(self, s: str) -> list[str]: 27 """ 28 This function decodes the encoded string. The way it does this is by 29 first parsing digits until it doesn't see any digits, and saving that result. 30 Then parsing the hashtag. 31 Then taking that many chars from the string, and adding it to the list of strings. 32 And then advancing the string by that much. 33 """ 34 strs = [] 35 36 while s: 37 num, num_len = self.tok_number(s) 38 s = s[num_len:] 39 self.tok_delimiter(s) 40 s = s[1:] 41 parsed = self.take_chars(num, s) 42 s = s[num:] 43 strs.append(parsed) 44 45 return strs
This function decodes the encoded string. The way it does this is by first parsing digits until it doesn't see any digits, and saving that result. Then parsing the hashtag. Then taking that many chars from the string, and adding it to the list of strings. And then advancing the string by that much.