Skip to content

Instantly share code, notes, and snippets.

@mthelander
Created January 8, 2012 01:37
Show Gist options
  • Save mthelander/1576765 to your computer and use it in GitHub Desktop.
Save mthelander/1576765 to your computer and use it in GitHub Desktop.
longest palindrome finder
def find_longest_palindrome(string)
(string.length/2).downto(1) do |i|
backrefs = "\\#{(-i..-1).map(&:abs).join("\\").sub(i.to_s, "#{i}?")}"
string.scan(/#{'(.)'*i}(#{backrefs})/).tap do |a|
return a.take(i).join unless a.empty?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment