Skip to content

Instantly share code, notes, and snippets.

@gagaception
Created November 4, 2015 19:22
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/6c2be20e9e4807a48628 to your computer and use it in GitHub Desktop.
Save gagaception/6c2be20e9e4807a48628 to your computer and use it in GitHub Desktop.
module LongestSubstring
def self.find(first, second)
return nil if first.nil? || second.nil?
return nil if first != second
x, xs, y, ys = first[0..0], first[1..-1], second[0..0], second[1..-1]
if x == y
x + self.find(xs, ys)
else
[self.find(first, ys), self.find(xs, second)].max_by {|x| x.size}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment