programming-examples/ruby/Algorithms/longest_common_prefix.rb

9 lines
215 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# @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