Simple Ruby Solution


  • 0
    T
    # Encodes a list of strings to a single string.
    #
    # @param {string[]} strs
    # @return {string}
    def encode(strs)
        (strs + [" "]).map { |c| escape(c) }.join('XX')
    end
    
    def escape(c)
        c.gsub('X', 'Xi')
    end
    
    # Decodes a single string to a list of strings.
    #
    # @param {string} s
    # @return {string[]}
    def decode(s)
        s.split('XX').map {|c| c.gsub('Xi', 'X')}[0..-2]
    end
    

Log in to reply
 

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