Created
October 5, 2014 15:03
-
-
Save jleeothon/8396a43646648f251dec to your computer and use it in GitHub Desktop.
Ruby: lowest common ancestor (recursive)
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
def lcs s1, s2 | |
if s1.empty? or s2.empty? | |
return '' | |
elsif s1[0] == s2[0] | |
return s1[0] + lcs(s1[1..-1], s2[1..-1]) | |
end | |
left = lcs s1[1..-1], s2 | |
right = lcs s1, s2[1..-1] | |
return left.size > right.size ? left : right | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment