Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string ""
def longest_common_prefix(strs)
return "" if strs.empty?
end
def longest_common_prefix(strs)
return "" if strs.empty?
s = ""
base = strs[0]
for i in 0..base.length-1
end
def longest_common_prefix(strs)
return "" if strs.empty?
s = ""
base = strs[0]
for i in 0..base.length-1
if strs.all?{|x| x[i] == base[i]}
s += base[i]
def longest_common_prefix(strs)
return "" if strs.empty?
s = ""
base = strs[0]
for i in 0..base.length-1
if strs.all?{|x| x[i] == base[i]}
s += base[i]
else
break
end
end
s
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store