programming-examples/ruby/Algorithms/longest_common_prefix.rb
2019-11-15 12:59:38 +01:00

9 lines
215 B
Ruby

# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
return '' if strs.empty?
min, max = strs.minmax
idx = min.size.times { |i| break i unless min[i] == max[i] }
min[0...idx]
end