Skip to content

Instantly share code, notes, and snippets.

@itssomething
Created January 23, 2024 20:02
Show Gist options
  • Save itssomething/4c96c1fd221787b71031894c51d2e050 to your computer and use it in GitHub Desktop.
Save itssomething/4c96c1fd221787b71031894c51d2e050 to your computer and use it in GitHub Desktop.
# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
strs = strs.sort
first = strs[0]
last = strs[strs.length - 1]
string = ""
first.length.times do |index|
if first[index] == last[index]
string += first[index]
else
break
end
end
return string
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment