Skip to content

Instantly share code, notes, and snippets.

@gagaception
Created November 4, 2015 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagaception/7536548cc59d18339224 to your computer and use it in GitHub Desktop.
Save gagaception/7536548cc59d18339224 to your computer and use it in GitHub Desktop.
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