Created
January 8, 2012 01:37
-
-
Save mthelander/1576765 to your computer and use it in GitHub Desktop.
longest palindrome finder
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 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