My accepted Python Solution


  • 0
    Y
        # The way to separate strs: 
        #     1. When encoding, put the length and a space before each str
        #     2. When decoding, read the length n of str, and then read n chars into str 
    
        def encode(self, strs):
            result = ''
            for s in strs:
                result += (str(len(s)) + ' ' + s)
                
            return result
        
        def decode(self, s):
            result = []
            i = 0
            while i < len(s):
                n = 0
                current = ''
                while s[i].isdigit():
                    n = n*10 + int(s[i])
                    i += 1
                
                i += 1  # skip the splitting space
    
                for j in range(n):
                    current += s[i]
                    i += 1
                    
                result.append(current)
                
            return result

  • 0
    L

    I was wondering why you have yo put the length and a space before each str


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.