Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- """
- @param: strs: a list of strings
- @return: encodes a list of strings to a single string.
- Thought process: If we just use a delimiter we always risk having that
- delimiter show up in our input.
- So the safest way to do it is to read in numbers until we hit a known delimiter,
- then convert those characters to a number, and read in that many characters
- for the next string.
- """
- def encode(self, strs):
- output = ''
- for item in strs:
- output += str(len(item)) + '#' + item
- return output
- """
- @param: str: A string
- @return: dcodes a single string to a list of strings
- """
- def decode(self, str):
- # write your code
- i = j = 0
- print(str)
- output = []
- while j < len(str):
- if str[j] == '#':
- print(i, j)
- next_string_start_index = j + 1
- next_string_end_index = next_string_start_index + int(str[i:j]) - 1
- output.append(str[next_string_start_index:next_string_end_index + 1])
- i = next_string_end_index + 1
- j = i + 1
- continue
- j += 1
- return output
Advertisement
Add Comment
Please, Sign In to add comment