HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

HackerRank challenge - index palindrome

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
hackerrankindexpalindromechallenge

Problem

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

Solution

First of all, your palindrome function uses inefficient logic. Reversing a string takes N steps, where N is the length of the string. Comparing a string to its reverse takes some additional steps, though typically not that many for ordinary strings, near N / 2 steps for almost-palindromes, N steps for palindromes, but that's not a real concern.

The important thing is that you can check if a string is palindrome in N / 2 steps by comparing the ith character from start and end. The even more important thing is that you can use this logic to fond the index that breaks a palindrome, significantly speeding up your parse function.

As a final remark about good coding practices, your parse function does too many things, and it's also poorly named. It would be better to decompose this to multiple functions that each do one logical thing, and name them accordingly.

Context

StackExchange Code Review Q#91779, answer score: 3

Revisions (0)

No revisions yet.