Faaez Razeen

Encode and Decode Strings

  • 1 min read
  • Array
  • String
  • LC-Easy
  • Blind75

3 years ago

Approach

def encode(.strs: List[str]) -> str: return ''.join([f'{len(word)}#{word}' for word in strs]) def decode(s: str) -> List[str]: ans = [] i = 0 while i < len(s): word_len = '' while s[i] != '#': word_len += s[i] i += 1 word_len = int(word_len) ans.append(s[i + 1: i + word_len + 1]) i = i + word_len + 1 return ans