I have a solution but it fails the performance for two tests. How can I speed it up?
def palindrome?(str)
str.reverse == str
end
def parse(str)
return -1 if palindrome?(str)
str.length.times do |i|
before = str[0...i]
after = str[i+1...str.length]
return i if palindrome?(before + after)
end
end
tests = gets.strip.to_i
tests.times do
str = gets.strip
puts parse(str)
end
I tried replacing my palindrome checker function but using benchmarking this seems to actually execute slower:
def palindrome?(str)
(str.length/2).times do |i|
return false if str[i] != str[-(i+1)]
end
true
end
enter code here