Created
November 4, 2015 19:54
-
-
Save gagaception/7536548cc59d18339224 to your computer and use it in GitHub Desktop.
This file contains hidden or 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(first, second) | |
return nil if first.nil? || second.nil? | |
x, xs, y, ys = first[0..0], first[1..-1], second[0..0], second[1..-1] | |
if x == y | |
x + lcs(xs, ys) | |
else | |
[lcs(first, ys), lcs(xs, second)].max_by {|x| x.size} | |
end | |
end | |
puts lcs('ABCDAFvvB', 'ACBCFvcB') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment