Created
January 23, 2024 20:02
-
-
Save itssomething/4c96c1fd221787b71031894c51d2e050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @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