Find the longest palindrome substring

Using Ruby…

def longest(string)
string.length.downto(2) do |pal_length|
0.upto(string.length - pal_length) do |index|
pal = string[index, pal_length]
return pal if is_pal?(pal)
end
end
1
end
def is_pal?(string)
0.upto(string.length / 2.0) do |i|
return false if string[i] != string[string.length - i - 1]
end
true
end
puts longest("foofi") # "foof"
puts longest("boyob") # "boyob"
puts longest("lallal") # "lallal"

Advocate of Score Voting and Approval Voting. Software engineer. Father. Husband. American.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store