fork download
  1. class Solution:
  2. def encode(self, strs: list[str]) -> str:
  3. # Efficiently join all "length:string" segments
  4. return "".join(f"{len(s)}:{s}" for s in strs)
  5.  
  6. def decode(self, s: str) -> list[str]:
  7. decoded = []
  8. i = 0
  9. while i < len(s):
  10. # find the colon
  11. j = s.find(":", i)
  12. length = int(s[i:j]) # parse length
  13. word = s[j+1:j+1+length] # extract word
  14. decoded.append(word)
  15. i = j + 1 + length # move to next segment
  16. return decoded
  17.  
Success #stdin #stdout 0.07s 14068KB
stdin
Standard input is empty
stdout
Standard output is empty