Skip to main content
2 of 3
deleted 4 characters in body; edited tags; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

HackerRank challenge - index palindrome

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
osman
  • 143
  • 7