Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Find the Maximum Length Palindrome in a String Provide a method that takes a string and returns the maximum length palindrome. A palindrome is a word, phrase or number that can be read the same in either direction.
def max_palindrome(str)
i,size = 0,str.length
until palindrome?(str,i,size) do
if i < str.length-size then i=i+1 else i=0; size=size-1 end
def palindrome?(str,i,size)
(i..i+size/2-1).each{ |j| if str[j] != str[2*i+size-j-1] then return false end }
puts "Max Palindrome: #{max_palindrome("abcba")}"
puts "Max Palindrome: #{max_palindrome("aabcbdb")}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment