Ruby code


  • 0
    P
    def longest_common_prefix(strs)
        if strs.nil? || strs.empty?
          return ''
        end
        prefix = strs.min { |x, y| x.size <=> y.size }
    
        strs.each do |i|
          while i.index(prefix) != 0
              prefix = prefix[0...prefix.length-1]
            end
          end
        prefix
      end
    ``

Log in to reply
 

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